Foundations introduced dict comprehensions briefly. Today's lesson is the deeper view — what shape they accept, common patterns, and when to use a for loop instead.
words = ["a", "bb", "ccc"]
lengths = {w: len(w) for w in words}
print(lengths) # {'a': 1, 'bb': 2, 'ccc': 3}The key: value form on the left, the for source on the right. Like a list comprehension but with a colon in the expression.
Right. Reads: "a dict of w: len(w) for each w in words." Both the key expression and the value expression can use the loop variable.
What if I want to build a dict from two parallel lists?
zip them, then comprehend:
keys = ["a", "b", "c"]
values = [1, 2, 3]
{k: v for k, v in zip(keys, values)} # {'a': 1, 'b': 2, 'c': 3}(Or directly: dict(zip(keys, values)) — same result, even shorter, but the comprehension form scales when you also want a transform.)
And filtering?
Same if clause as list comprehensions — at the end:
{k: v for k, v in pairs if v > 0} # only positive values{key_expr: value_expr for item in iterable}
{key_expr: value_expr for item in iterable if condition}words = ["a", "bb", "ccc"]
{w: len(w) for w in words} # {'a': 1, 'bb': 2, 'ccc': 3}ages = {"Ada": 36, "Bob": 41}
{name: age + 1 for name, age in ages.items()} # {'Ada': 37, 'Bob': 42}ages = {"Ada": 36, "Bob": 41, "Cleo": 17}
{name: age for name, age in ages.items() if age >= 18}
# {'Ada': 36, 'Bob': 41}keys = ["a", "b", "c"]
vals = [1, 2, 3]
{k: v for k, v in zip(keys, vals)} # {'a': 1, 'b': 2, 'c': 3}ages = {"Ada": 36, "Bob": 41}
{age: name for name, age in ages.items()}
# {36: 'Ada', 41: 'Bob'}Watch out: if two source values are equal, you'll lose one key — the second one wins.
{k: print(k) for k in keys} # builds a dict of None — use a for loopIf the per-item logic doesn't return a useful value, use a regular for loop.
Sets get a comprehension too — curly brackets, no colon:
{n * 2 for n in [1, 1, 2, 3]} # {2, 4, 6}| You want | Use |
|---|---|
| List of values | [expr for x in xs] |
| Set (no duplicates) | {expr for x in xs} |
| Dict (key→value) | {k: v for x in xs} |
| The same logic with side effects | regular for loop |
Foundations introduced dict comprehensions briefly. Today's lesson is the deeper view — what shape they accept, common patterns, and when to use a for loop instead.
words = ["a", "bb", "ccc"]
lengths = {w: len(w) for w in words}
print(lengths) # {'a': 1, 'bb': 2, 'ccc': 3}The key: value form on the left, the for source on the right. Like a list comprehension but with a colon in the expression.
Right. Reads: "a dict of w: len(w) for each w in words." Both the key expression and the value expression can use the loop variable.
What if I want to build a dict from two parallel lists?
zip them, then comprehend:
keys = ["a", "b", "c"]
values = [1, 2, 3]
{k: v for k, v in zip(keys, values)} # {'a': 1, 'b': 2, 'c': 3}(Or directly: dict(zip(keys, values)) — same result, even shorter, but the comprehension form scales when you also want a transform.)
And filtering?
Same if clause as list comprehensions — at the end:
{k: v for k, v in pairs if v > 0} # only positive values{key_expr: value_expr for item in iterable}
{key_expr: value_expr for item in iterable if condition}words = ["a", "bb", "ccc"]
{w: len(w) for w in words} # {'a': 1, 'bb': 2, 'ccc': 3}ages = {"Ada": 36, "Bob": 41}
{name: age + 1 for name, age in ages.items()} # {'Ada': 37, 'Bob': 42}ages = {"Ada": 36, "Bob": 41, "Cleo": 17}
{name: age for name, age in ages.items() if age >= 18}
# {'Ada': 36, 'Bob': 41}keys = ["a", "b", "c"]
vals = [1, 2, 3]
{k: v for k, v in zip(keys, vals)} # {'a': 1, 'b': 2, 'c': 3}ages = {"Ada": 36, "Bob": 41}
{age: name for name, age in ages.items()}
# {36: 'Ada', 41: 'Bob'}Watch out: if two source values are equal, you'll lose one key — the second one wins.
{k: print(k) for k in keys} # builds a dict of None — use a for loopIf the per-item logic doesn't return a useful value, use a regular for loop.
Sets get a comprehension too — curly brackets, no colon:
{n * 2 for n in [1, 1, 2, 3]} # {2, 4, 6}| You want | Use |
|---|---|
| List of values | [expr for x in xs] |
| Set (no duplicates) | {expr for x in xs} |
| Dict (key→value) | {k: v for x in xs} |
| The same logic with side effects | regular for loop |
Create a free account to get started. Paid plans unlock all tracks.