You have two lists of category strings — ["food", "travel"] and ["travel", "work"] — and you want every distinct category across both, sorted: ["food", "travel", "work"]. Duplicates across the two lists collapse. What's the operation?
Concatenate them and then deduplicate? list_a + list_b, convert to set, sort?
That works. But there's a shorter path — the | operator, set union, does concatenate-and-dedupe in one step:
set(["food", "travel"]) | set(["travel", "work"])
# {"food", "travel", "work"}Union means "everything that's in either set" — duplicates across the two inputs appear once in the result. No manual dedup needed.
And the same sort wrapper from yesterday gives a deterministic list?
Same pattern. Sets have no order; sorted() converts to a list and orders the values:
def combine_unique_items(a: list, b: list) -> list:
return sorted(set(a) | set(b))The operator | does double duty: it combines the two sets AND deduplicates. You can't do that in one step with lists.
What if one list is empty? Does the union just return the other list's values?
Empty set unioned with anything gives back the other set. set() | {"a", "b"} is {"a", "b"}. Same for the other direction. No special case — the math handles it. And if both are empty, you get an empty set, which sorts to [].
So yesterday's & was "in both" and today's | is "in either". Two operators, two fundamental questions about two collections.
Plus - for "in this one but not the other" and ^ for "in exactly one". Four operators, every combination question you'll ever ask. And they all work the same way: convert inputs to sets, apply the operator, sort the result.
TL;DR: set(a) | set(b) returns all unique elements from both — concatenate plus dedupe in one operation.
| operator — union; returns a seta | b equals b | a| Question | Operator |
|---|---|
| In both? | a & b |
| In either? | a | b |
| Only in a? | a - b |
| In exactly one? | a ^ b |
Union and intersection are the two workhorses; difference and symmetric difference handle the "only in" and "XOR" cases.
You have two lists of category strings — ["food", "travel"] and ["travel", "work"] — and you want every distinct category across both, sorted: ["food", "travel", "work"]. Duplicates across the two lists collapse. What's the operation?
Concatenate them and then deduplicate? list_a + list_b, convert to set, sort?
That works. But there's a shorter path — the | operator, set union, does concatenate-and-dedupe in one step:
set(["food", "travel"]) | set(["travel", "work"])
# {"food", "travel", "work"}Union means "everything that's in either set" — duplicates across the two inputs appear once in the result. No manual dedup needed.
And the same sort wrapper from yesterday gives a deterministic list?
Same pattern. Sets have no order; sorted() converts to a list and orders the values:
def combine_unique_items(a: list, b: list) -> list:
return sorted(set(a) | set(b))The operator | does double duty: it combines the two sets AND deduplicates. You can't do that in one step with lists.
What if one list is empty? Does the union just return the other list's values?
Empty set unioned with anything gives back the other set. set() | {"a", "b"} is {"a", "b"}. Same for the other direction. No special case — the math handles it. And if both are empty, you get an empty set, which sorts to [].
So yesterday's & was "in both" and today's | is "in either". Two operators, two fundamental questions about two collections.
Plus - for "in this one but not the other" and ^ for "in exactly one". Four operators, every combination question you'll ever ask. And they all work the same way: convert inputs to sets, apply the operator, sort the result.
TL;DR: set(a) | set(b) returns all unique elements from both — concatenate plus dedupe in one operation.
| operator — union; returns a seta | b equals b | a| Question | Operator |
|---|---|
| In both? | a & b |
| In either? | a | b |
| Only in a? | a - b |
| In exactly one? | a ^ b |
Union and intersection are the two workhorses; difference and symmetric difference handle the "only in" and "XOR" cases.
Write `combine_unique_items(a, b)` that returns a sorted list of every distinct value from both `a` and `b`. Duplicates across the lists should appear only once. Use set union.
Tap each step for scaffolded hints.
No blank-editor panic.