← Python

Dictionaries – Iteration

Iterating a dictionary with a for loop visits each key in turn

What it does: .items() provides (key, value) tuples. k, v unpacks each pair. 'in' checks if a key exists in the dict — prevents KeyError. Iterating a dict directly gives you the keys — same as for key in d.keys():. You must call .values() explicitly — for v in d: gives keys, not values. .items() returns (key, value) pairs — then unpack them into k and v. Looping over d directly yields the keys — 'a' and 'b', not the values. Python uses the in operator — dicts don't have a .has() method. Changing a dict's size mid-loop raises RuntimeError — loop a copy instead.

Common mistakes: Looping over d directly gives only keys. Use .items() for key-value pairs. for val in d: gives KEYS, not values. Use .values() for values.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
for key in d: what does key get?
Each key, one at a timeEach value
Matches
❌ d = {'a':1} for k, v in d: print(k, v)
Error: Must use d.items()
Applying
Meanings
Term
for key in d:
Meaning
Iterate through all keys in the dictionary.
Practice
d = {'a': 1, 'b': 2, 'c': 3}
Testing
Gaps
for key d:
Check Answer
Spots
1d = {'a': 1, 'b': 2}
2for k, v in d:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Ermine' badge — Tundra Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. for key in d: what does key get?
A. Each key, one at a time ✓
B. Each value
Iterating a dict directly gives you the keys — same as for key in d.keys():
2. How do you loop through values?
A. for v in d:
B. for v in d.values(): ✓
You must call .values() explicitly — for v in d: gives keys, not values
3. How do you loop through keys AND values?
A. for k, v in d.items(): ✓
B. for k, v in d:
.items() returns (key, value) pairs — then unpack them into k and v
4. d = {'a':1, 'b':2}\nfor k in d:\n print(k)\nWhat prints?
A. 1 then 2
B. a then b ✓
Looping over d directly yields the keys — 'a' and 'b', not the values
5. d = {'x':10}\nfor k, v in d.items():\n print(k, v)\nWhat prints?
A. x 10 ✓
B. x
.items() unpacks each pair — k gets 'x' and v gets 10

Match the Pairs

d = {'a':1} for k, v in d: print(k, v) Error: Must use d.items()
d = {'x':1} d['x'] += 1 del d['x'] print(d['x']) Error: Key deleted, KeyError
for k, v in d.items(): print(k, v) Valid: Unpack key and value
if 'name' in d: print(d['name']) Ok: Check key before accessing
d = {'a':1,'b':2} for k in d: print(k) Output: a b
d = {'x':10} for k,v in d.items(): print(k,v) Output: x 10

Key Term

for key in d:
Iterate through all keys in the dictionary.~~for v in d.values():

Fill in the Blanks

1. for key in d: print(key)
2. for val in d.values(): print(val)
3. for k, v in d.items(): print(k, v)
4. if 'name' in d: print('Found')
5. for key in d: print(d[key])

Spot the Error

Question 1
A d = {'a': 1, 'b': 2}
B for k, v in d:
C print(k, v)
D print('Done')
Line B — Looping over d directly gives only keys. Use .items() for key-value pairs.
Question 2
A d = {'a': 1, 'b': 2}
B for val in d:
C print(val)
D # expects values
Line B — for val in d: gives KEYS, not values. Use .values() for values.
Question 3
A d = {'a': 1, 'b': 2}
B for k, v in d.keys():
C print(k, v)
D print('Done')
Line B — .keys() only gives keys. Use .items() to get both key and value.
Question 4
A d = {'a': 1, 'b': 2}
B for k in d.items():
C print(k)
D print('Done')
Line B — Without unpacking, k is a tuple like ('a', 1). Use k, v to unpack.
Question 5
A found = 'Alice' in {'name': 'Alice', 'age': 12}
B d = {'name': 'Alice', 'age': 12}
C print('Found:', found)
D # expects True
Line A — 'in' checks KEYS by default. To check values, use d.values().
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?