List comprehensions build a list. Dicts have the same shape:
ages = {"Ada": 36, "Bob": 41, "Cleo": 28}
# Goal: build a new dict where each age is doubledA dict comprehension has two expressions, separated by :, mapped from key: value:
doubled = {name: age * 2 for name, age in ages.items()}
print(doubled) # {'Ada': 72, 'Bob': 82, 'Cleo': 56}Read it: "a dict of name: age * 2 for each name, age pair in ages.items()."
And to get a dict from two parallel lists?
Use zip to pair them up:
names = ["Ada", "Bob", "Cleo"]
ages = [36, 41, 28]
lookup = {n: a for n, a in zip(names, ages)}
print(lookup) # {'Ada': 36, 'Bob': 41, 'Cleo': 28}zip produces pairs ("Ada", 36), ("Bob", 41), ... — the comprehension turns those into key-value pairs.
{key_expr: value_expr for item in iterable}Read as: "a dict of key_expr: value_expr for each item in iterable."
ages = {"Ada": 36, "Bob": 41, "Cleo": 28}
# Double every value
{n: a * 2 for n, a in ages.items()}
# {'Ada': 72, 'Bob': 82, 'Cleo': 56}
# Uppercase every key, keep the value
{n.upper(): a for n, a in ages.items()}
# {'ADA': 36, 'BOB': 41, 'CLEO': 28}names = ["Ada", "Bob", "Cleo"]
ages = [36, 41, 28]
{n: a for n, a in zip(names, ages)}
# {'Ada': 36, 'Bob': 41, 'Cleo': 28}zip turns (names, ages) into an iterable of pairs. The comprehension binds each pair to n, a and emits n: a.
if clause# Only entries where age > 30
{n: a for n, a in ages.items() if a > 30}
# {'Ada': 36, 'Bob': 41}Same rule as list comprehensions: if the per-item logic has more than one expression or has side effects, use a regular for loop. Comprehensions are for transforms, not for general iteration.
Sets follow the same shape as lists, just curly brackets without the key::
{n * 2 for n in [1, 2, 2, 3, 3]} # {2, 4, 6} — duplicates droppedThree comprehension shapes — list [...], set {...}, dict {... : ...}.
Create a free account to get started. Paid plans unlock all tracks.
List comprehensions build a list. Dicts have the same shape:
ages = {"Ada": 36, "Bob": 41, "Cleo": 28}
# Goal: build a new dict where each age is doubledA dict comprehension has two expressions, separated by :, mapped from key: value:
doubled = {name: age * 2 for name, age in ages.items()}
print(doubled) # {'Ada': 72, 'Bob': 82, 'Cleo': 56}Read it: "a dict of name: age * 2 for each name, age pair in ages.items()."
And to get a dict from two parallel lists?
Use zip to pair them up:
names = ["Ada", "Bob", "Cleo"]
ages = [36, 41, 28]
lookup = {n: a for n, a in zip(names, ages)}
print(lookup) # {'Ada': 36, 'Bob': 41, 'Cleo': 28}zip produces pairs ("Ada", 36), ("Bob", 41), ... — the comprehension turns those into key-value pairs.
{key_expr: value_expr for item in iterable}Read as: "a dict of key_expr: value_expr for each item in iterable."
ages = {"Ada": 36, "Bob": 41, "Cleo": 28}
# Double every value
{n: a * 2 for n, a in ages.items()}
# {'Ada': 72, 'Bob': 82, 'Cleo': 56}
# Uppercase every key, keep the value
{n.upper(): a for n, a in ages.items()}
# {'ADA': 36, 'BOB': 41, 'CLEO': 28}names = ["Ada", "Bob", "Cleo"]
ages = [36, 41, 28]
{n: a for n, a in zip(names, ages)}
# {'Ada': 36, 'Bob': 41, 'Cleo': 28}zip turns (names, ages) into an iterable of pairs. The comprehension binds each pair to n, a and emits n: a.
if clause# Only entries where age > 30
{n: a for n, a in ages.items() if a > 30}
# {'Ada': 36, 'Bob': 41}Same rule as list comprehensions: if the per-item logic has more than one expression or has side effects, use a regular for loop. Comprehensions are for transforms, not for general iteration.
Sets follow the same shape as lists, just curly brackets without the key::
{n * 2 for n in [1, 2, 2, 3, 3]} # {2, 4, 6} — duplicates droppedThree comprehension shapes — list [...], set {...}, dict {... : ...}.