You have a list of transaction dicts — [{"category": "Food"}, {"category": "Gas"}, {"category": "Food"}] — and you want the distinct categories, sorted alphabetically: ["Food", "Gas"]. Deduplication plus ordering. What's the cleanest pipeline?
A set to dedupe, then sorted() to order. But I've only built sets with the set() constructor. Is there a comprehension form?
There is. Same braces as a dict, but no colon — just the expression before for:
# Dict comprehension: {k: v for ...}
# Set comprehension: {x for ...} — no colon, just the valuePython distinguishes them by the colon. No colon means set, colon means dict. Duplicates collapse automatically because that's what sets do.
So {t["category"] for t in transactions} pulls out the category strings and dedupes them in one step?
Exactly the shape. Extract, dedupe, done. Then sorted() gives you a deterministic list to return:
def unique_categories(transactions: list) -> list:
return sorted({t["category"] for t in transactions})Set comprehension on the inside, sorted() on the outside. The set handles uniqueness; the sort handles order.
Why convert to a sorted list at the end — can't I just return the set?
Sets have no order — iterating over them twice can produce different sequences. For a return value your caller will print, compare, or serialize, sorted() gives a predictable shape. And sorted() on a set works the same as on any iterable — it returns a list.
And for an empty list, I get an empty list back? Comprehensions degrade gracefully everywhere.
Empty in, empty out. The set comprehension is also the idiomatic way Python programmers express "give me the distinct values" — clearer than calling set() on a list after the fact.
TL;DR: {x for x in xs} builds a set — duplicates collapse automatically.
{} braces| Form | Meaning |
|---|---|
{x for x in xs} | set |
{k: v for k, v in pairs} | dict |
[x for x in xs] | list |
The colon is the only syntactic cue between a dict and a set comprehension.
You have a list of transaction dicts — [{"category": "Food"}, {"category": "Gas"}, {"category": "Food"}] — and you want the distinct categories, sorted alphabetically: ["Food", "Gas"]. Deduplication plus ordering. What's the cleanest pipeline?
A set to dedupe, then sorted() to order. But I've only built sets with the set() constructor. Is there a comprehension form?
There is. Same braces as a dict, but no colon — just the expression before for:
# Dict comprehension: {k: v for ...}
# Set comprehension: {x for ...} — no colon, just the valuePython distinguishes them by the colon. No colon means set, colon means dict. Duplicates collapse automatically because that's what sets do.
So {t["category"] for t in transactions} pulls out the category strings and dedupes them in one step?
Exactly the shape. Extract, dedupe, done. Then sorted() gives you a deterministic list to return:
def unique_categories(transactions: list) -> list:
return sorted({t["category"] for t in transactions})Set comprehension on the inside, sorted() on the outside. The set handles uniqueness; the sort handles order.
Why convert to a sorted list at the end — can't I just return the set?
Sets have no order — iterating over them twice can produce different sequences. For a return value your caller will print, compare, or serialize, sorted() gives a predictable shape. And sorted() on a set works the same as on any iterable — it returns a list.
And for an empty list, I get an empty list back? Comprehensions degrade gracefully everywhere.
Empty in, empty out. The set comprehension is also the idiomatic way Python programmers express "give me the distinct values" — clearer than calling set() on a list after the fact.
TL;DR: {x for x in xs} builds a set — duplicates collapse automatically.
{} braces| Form | Meaning |
|---|---|
{x for x in xs} | set |
{k: v for k, v in pairs} | dict |
[x for x in xs] | list |
The colon is the only syntactic cue between a dict and a set comprehension.
Write `unique_categories(transactions)` that returns a sorted list of distinct `category` values from a list of transaction dicts. Use a set comprehension, then sort the result.
Tap each step for scaffolded hints.
No blank-editor panic.