Given [1, 2, 3, 4, 5], you want only the even numbers squared — so [4, 16]. Two things happen: filter out odd numbers, then square the rest. How would you tighten that into a single expression?
Filter and transform feel like two passes. Wouldn't I need an if check, then a separate square step?
Comprehensions fuse them. The syntax adds an if clause at the end: [expr for x in xs if condition]. The condition runs before expr — only items that pass get transformed and collected.
# Two-pass loop version
result = []
for x in nums:
if x % 2 == 0:
result.append(x * x)One loop, one conditional append. The comprehension is the same logic in one line.
So the if goes at the end, after the for? Not in the middle next to expr?
Right at the end. Read left to right: "x * x, for each x in nums, if x % 2 == 0". The filter is the last clause because conceptually it gates which items reach the expression:
def square_even_numbers(nums: list) -> list:
return [x * x for x in nums if x % 2 == 0]Odd numbers never hit x * x — they're filtered out before the expression runs.
What if no numbers match the filter — say all of them are odd?
The result is []. Same behavior as an empty input list. The comprehension naturally produces the empty case with no special handling. And x * x versus x ** 2 — both work. x ** 2 is the exponent operator; x * x is just multiplication. For squaring, they're equivalent.
So every filter-then-transform loop I've ever written can collapse to one line with an if clause?
That's the pattern. Filter on the right, transform on the left. Every variation is just a different predicate or expression. This one shape covers a huge chunk of real data work.
TL;DR: [expr for x in xs if cond] filters with if, then transforms with expr — in one line.
if cond gates which items reach exprexpr, then for, then if| Input | [x*x for x in xs if x % 2 == 0] |
|---|---|
[1, 2, 3, 4] | [4, 16] |
[1, 3, 5] | [] |
[] | [] |
The if is a guard on each iteration, not a conditional expression inside expr.
Given [1, 2, 3, 4, 5], you want only the even numbers squared — so [4, 16]. Two things happen: filter out odd numbers, then square the rest. How would you tighten that into a single expression?
Filter and transform feel like two passes. Wouldn't I need an if check, then a separate square step?
Comprehensions fuse them. The syntax adds an if clause at the end: [expr for x in xs if condition]. The condition runs before expr — only items that pass get transformed and collected.
# Two-pass loop version
result = []
for x in nums:
if x % 2 == 0:
result.append(x * x)One loop, one conditional append. The comprehension is the same logic in one line.
So the if goes at the end, after the for? Not in the middle next to expr?
Right at the end. Read left to right: "x * x, for each x in nums, if x % 2 == 0". The filter is the last clause because conceptually it gates which items reach the expression:
def square_even_numbers(nums: list) -> list:
return [x * x for x in nums if x % 2 == 0]Odd numbers never hit x * x — they're filtered out before the expression runs.
What if no numbers match the filter — say all of them are odd?
The result is []. Same behavior as an empty input list. The comprehension naturally produces the empty case with no special handling. And x * x versus x ** 2 — both work. x ** 2 is the exponent operator; x * x is just multiplication. For squaring, they're equivalent.
So every filter-then-transform loop I've ever written can collapse to one line with an if clause?
That's the pattern. Filter on the right, transform on the left. Every variation is just a different predicate or expression. This one shape covers a huge chunk of real data work.
TL;DR: [expr for x in xs if cond] filters with if, then transforms with expr — in one line.
if cond gates which items reach exprexpr, then for, then if| Input | [x*x for x in xs if x % 2 == 0] |
|---|---|
[1, 2, 3, 4] | [4, 16] |
[1, 3, 5] | [] |
[] | [] |
The if is a guard on each iteration, not a conditional expression inside expr.
Write `square_even_numbers(nums)` that returns a list of squares of only the even numbers in `nums`. Use a list comprehension with an `if` clause.
Tap each step for scaffolded hints.
No blank-editor panic.