A dict has keys and values. A for loop has one variable. How do you walk every pair?
Loop over the keys, then look up the value inside?
That works:
for key in ages:
print(key, ages[key])By default, iterating a dict gives you its keys. But there's a tidier way — .items() returns each (key, value) pair, and for can unpack two variables at once:
for key, value in ages.items():
print(f"{key}: {value}")Two variables on the left of in — that's new.
Tuple unpacking. .items() yields pairs like ("Ada", 36), and Python splits the pair into two names automatically. Concise, readable, idiomatic.
And .values() — gives just the values?
Right. Three options: for key in dict: (keys), for value in dict.values(): (values only), for key, value in dict.items(): (both). Pick what the loop body actually needs.
Three ways to loop over a dict, depending on what the body needs:
ages = {"Ada": 36, "Bob": 41}
for key in ages:
print(key)
# Ada
# BobIterating a dict directly yields its keys. (You can also write for key in ages.keys(): — same thing, more explicit.)
for value in ages.values():
print(value)
# 36
# 41.items()for key, value in ages.items():
print(f"{key}: {value}")
# Ada: 36
# Bob: 41.items() yields each pair as a 2-tuple ("Ada", 36). Python's for loop unpacks the pair into two names automatically.
Since Python 3.7, dicts preserve insertion order. Iterating yields pairs in the order they were added.
Build a transformed dict:
doubled = {}
for key, value in ages.items():
doubled[key] = value * 2Sum values:
total = 0
for value in ages.values():
total += valueFilter keys:
adults = []
for name, age in ages.items():
if age >= 18:
adults.append(name)Dicts + for + if covers a huge fraction of everyday Python.
A dict has keys and values. A for loop has one variable. How do you walk every pair?
Loop over the keys, then look up the value inside?
That works:
for key in ages:
print(key, ages[key])By default, iterating a dict gives you its keys. But there's a tidier way — .items() returns each (key, value) pair, and for can unpack two variables at once:
for key, value in ages.items():
print(f"{key}: {value}")Two variables on the left of in — that's new.
Tuple unpacking. .items() yields pairs like ("Ada", 36), and Python splits the pair into two names automatically. Concise, readable, idiomatic.
And .values() — gives just the values?
Right. Three options: for key in dict: (keys), for value in dict.values(): (values only), for key, value in dict.items(): (both). Pick what the loop body actually needs.
Three ways to loop over a dict, depending on what the body needs:
ages = {"Ada": 36, "Bob": 41}
for key in ages:
print(key)
# Ada
# BobIterating a dict directly yields its keys. (You can also write for key in ages.keys(): — same thing, more explicit.)
for value in ages.values():
print(value)
# 36
# 41.items()for key, value in ages.items():
print(f"{key}: {value}")
# Ada: 36
# Bob: 41.items() yields each pair as a 2-tuple ("Ada", 36). Python's for loop unpacks the pair into two names automatically.
Since Python 3.7, dicts preserve insertion order. Iterating yields pairs in the order they were added.
Build a transformed dict:
doubled = {}
for key, value in ages.items():
doubled[key] = value * 2Sum values:
total = 0
for value in ages.values():
total += valueFilter keys:
adults = []
for name, age in ages.items():
if age >= 18:
adults.append(name)Dicts + for + if covers a huge fraction of everyday Python.
Create a free account to get started. Paid plans unlock all tracks.