Five days of comprehensions. You've written list, dict, and set forms, and composed filter with transform. How does it sit?
The shape [f(x) for x in xs if cond] is muscle memory now. I catch myself writing loops and rewriting them as one-liners halfway through.
That's the reflex shift. Six questions to confirm — pay attention to the distinctions between list, dict, and set forms, and to where the if clause lives.
The one I'm still shaky on is dict versus set — both use braces and I keep mixing them up.
That'll show up in the quiz. Remember: colon = dict, no colon = set. Eighty percent to pass.
List comprehension: [expr for x in xs] — transform every item, return a new list.
Filter + transform: [expr for x in xs if cond] — the if clause gates which items reach expr. Filter lives at the end of the comprehension.
Dict comprehension: {key: value for x in xs} — the colon is the only syntactic cue distinguishing a dict from a set. Duplicate keys overwrite; last write wins.
Set comprehension: {x for x in xs} — same braces as a dict, no colon. Automatic deduplication.
Empty-input safety: every comprehension returns the empty shape ([], {}, empty set) on empty input. No guard clauses needed.
Five days of comprehensions. You've written list, dict, and set forms, and composed filter with transform. How does it sit?
The shape [f(x) for x in xs if cond] is muscle memory now. I catch myself writing loops and rewriting them as one-liners halfway through.
That's the reflex shift. Six questions to confirm — pay attention to the distinctions between list, dict, and set forms, and to where the if clause lives.
The one I'm still shaky on is dict versus set — both use braces and I keep mixing them up.
That'll show up in the quiz. Remember: colon = dict, no colon = set. Eighty percent to pass.
List comprehension: [expr for x in xs] — transform every item, return a new list.
Filter + transform: [expr for x in xs if cond] — the if clause gates which items reach expr. Filter lives at the end of the comprehension.
Dict comprehension: {key: value for x in xs} — the colon is the only syntactic cue distinguishing a dict from a set. Duplicate keys overwrite; last write wins.
Set comprehension: {x for x in xs} — same braces as a dict, no colon. Automatic deduplication.
Empty-input safety: every comprehension returns the empty shape ([], {}, empty set) on empty input. No guard clauses needed.