You have a list of transaction dicts, each with a "category" key, and you want only the ones where category == "Food". How would you filter?
I'd loop through the list, check each category, and append the matching ones to a new list. Three or four lines.
That works — and Python has a cleaner shorthand for exactly this shape. A list comprehension with an if clause does it in one line:
[t for t in txns if t["category"] == "Food"]Read it left-to-right: "give me each t in txns, but only if t["category"] equals "Food"."
And it returns a brand new list — it doesn't touch the original txns?
Correct. Comprehensions always build a new list. The input stays untouched, which makes them safe inside pipelines where multiple steps need to read the same source independently.
And what if nothing matches — no transaction has the right category?
You get an empty list. No special case needed — same as a loop where the if never fires. The comprehension handles empty gracefully:
def filter_by_category(txns, category):
return [t for t in txns if t["category"] == category]So one line of comprehension replaces three or four lines of loop. And it reads like a sentence.
That's why comprehensions are idiomatic Python. Once your eye parses [expr for item in iterable if condition] as "filtered transformation," you read these lines faster than the equivalent explicit loop.
TL;DR: [item for item in list if condition] — a filter in one line.
t for a simple filter)for item in iterable — the loopif condition — the filter (optional)| Form | Style |
|---|---|
| Loop + append | Four lines, explicit |
| Comprehension | One line, declarative |
Both produce the same result. Use comprehensions when the shape is "new list from old list" and the condition fits on one line.
Write `filter_by_category(txns, category)` that returns a new list of only the transaction dicts whose `"category"` matches.
Tap each step for scaffolded hints.
No blank-editor panic.
You have a list of transaction dicts, each with a "category" key, and you want only the ones where category == "Food". How would you filter?
I'd loop through the list, check each category, and append the matching ones to a new list. Three or four lines.
That works — and Python has a cleaner shorthand for exactly this shape. A list comprehension with an if clause does it in one line:
[t for t in txns if t["category"] == "Food"]Read it left-to-right: "give me each t in txns, but only if t["category"] equals "Food"."
And it returns a brand new list — it doesn't touch the original txns?
Correct. Comprehensions always build a new list. The input stays untouched, which makes them safe inside pipelines where multiple steps need to read the same source independently.
And what if nothing matches — no transaction has the right category?
You get an empty list. No special case needed — same as a loop where the if never fires. The comprehension handles empty gracefully:
def filter_by_category(txns, category):
return [t for t in txns if t["category"] == category]So one line of comprehension replaces three or four lines of loop. And it reads like a sentence.
That's why comprehensions are idiomatic Python. Once your eye parses [expr for item in iterable if condition] as "filtered transformation," you read these lines faster than the equivalent explicit loop.
TL;DR: [item for item in list if condition] — a filter in one line.
t for a simple filter)for item in iterable — the loopif condition — the filter (optional)| Form | Style |
|---|---|
| Loop + append | Four lines, explicit |
| Comprehension | One line, declarative |
Both produce the same result. Use comprehensions when the shape is "new list from old list" and the condition fits on one line.