Given ["dog", "cat", "hippo"], you want {"dog": 3, "cat": 3, "hippo": 5} — each word mapped to its length. A list comprehension returns a list, but you want a dict. What changes in the syntax?
Different brackets? Curly braces instead of square? But how does Python know the difference between a dict and a set — both use {}.
The colon. A dict comprehension has key: value before for; a set has just value. That colon is Python's signal that you're building key-value pairs. Here's the shape:
# List version (just lengths, no words): [len(w) for w in words]
# Dict version (pair each word with its length):
{w: len(w) for w in words}And for w in words works the same way — single iteration variable, same filter rules?
Exactly the same. Only the output shape changes. You can add an if clause too — {w: len(w) for w in words if len(w) > 3} keeps only words longer than 3 characters:
def word_lengths(words: list) -> dict:
return {w: len(w) for w in words}One line replaces four: init empty dict, iterate, assign each key-value pair, return.
What if the input has duplicates — ["cat", "cat", "dog"]? Two keys called "cat" would collide in the dict.
Later writes win. The second "cat" overwrites the first, but since they both have length 3, the output is the same. In general, dicts can't have duplicate keys — the comprehension just keeps the last value assigned to each key. Worth knowing before you debug a missing entry.
So anywhere I was building result = {} and assigning keys in a loop, I can collapse it to a comprehension?
That's the payoff. {k: v for k, v in pairs} for pair-based input; {x: f(x) for x in xs} for value-based. Same mental model as list comprehensions, different output shape.
TL;DR: {key: value for x in xs} builds a dict in one expression.
{k: v}) from set ({x}) in the same bracesif cond at the end if needed| Form | Output |
|---|---|
[len(w) for w in words] | list of lengths |
{w: len(w) for w in words} | dict of word→length |
The key: value before for is the only structural difference.
Given ["dog", "cat", "hippo"], you want {"dog": 3, "cat": 3, "hippo": 5} — each word mapped to its length. A list comprehension returns a list, but you want a dict. What changes in the syntax?
Different brackets? Curly braces instead of square? But how does Python know the difference between a dict and a set — both use {}.
The colon. A dict comprehension has key: value before for; a set has just value. That colon is Python's signal that you're building key-value pairs. Here's the shape:
# List version (just lengths, no words): [len(w) for w in words]
# Dict version (pair each word with its length):
{w: len(w) for w in words}And for w in words works the same way — single iteration variable, same filter rules?
Exactly the same. Only the output shape changes. You can add an if clause too — {w: len(w) for w in words if len(w) > 3} keeps only words longer than 3 characters:
def word_lengths(words: list) -> dict:
return {w: len(w) for w in words}One line replaces four: init empty dict, iterate, assign each key-value pair, return.
What if the input has duplicates — ["cat", "cat", "dog"]? Two keys called "cat" would collide in the dict.
Later writes win. The second "cat" overwrites the first, but since they both have length 3, the output is the same. In general, dicts can't have duplicate keys — the comprehension just keeps the last value assigned to each key. Worth knowing before you debug a missing entry.
So anywhere I was building result = {} and assigning keys in a loop, I can collapse it to a comprehension?
That's the payoff. {k: v for k, v in pairs} for pair-based input; {x: f(x) for x in xs} for value-based. Same mental model as list comprehensions, different output shape.
TL;DR: {key: value for x in xs} builds a dict in one expression.
{k: v}) from set ({x}) in the same bracesif cond at the end if needed| Form | Output |
|---|---|
[len(w) for w in words] | list of lengths |
{w: len(w) for w in words} | dict of word→length |
The key: value before for is the only structural difference.
Write `word_lengths(words)` that returns a dict mapping each word in `words` to its length. Use a dict comprehension.
Tap each step for scaffolded hints.
No blank-editor panic.