Dictionaries – Iteration

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Dictionaries – Iteration

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. for key in d: what does key get?
A Each key, one at a time
B Each value
2. How do you loop through values?
A for v in d.values():
B for v in d:
3. How do you loop through keys AND values?
A for k, v in d.items():
B for k, v in d:
4. d = {'a':1, 'b':2}\nfor k in d:\n print(k)\nWhat prints?
A 1 then 2
B a then b
5. d = {'x':10}\nfor k, v in d.items():\n print(k, v)\nWhat prints?
A x
B x 10

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1d = {'a':1} for k, v in d: print(k, v) AOutput: a b
2d = {'x':1} d['x'] += 1 del d['x'] print(d['x']) BError: Key deleted, KeyError
3for k, v in d.items(): print(k, v) COutput: x 10
4if 'name' in d: print(d['name']) DValid: Unpack key and value
5d = {'a':1,'b':2} for k in d: print(k) EOk: Check key before accessing
6d = {'x':10} for k,v in d.items(): print(k,v) FError: Must use d.items()

Definitions

for key in d:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

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

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A d = {'a': 1, 'b': 2}
B for k, v in d:
C print(k, v)
D print('Done')
Question 2
A d = {'a': 1, 'b': 2}
B for val in d:
C print(val)
D # expects values
Question 3
A d = {'a': 1, 'b': 2}
B for k, v in d.keys():
C print(k, v)
D print('Done')
Question 4
A d = {'a': 1, 'b': 2}
B for k in d.items():
C print(k)
D print('Done')
Question 5
A found = 'Alice' in {'name': 'Alice', 'age': 12}
B d = {'name': 'Alice', 'age': 12}
C print('Found:', found)
D # expects True

Answer Key

Dictionaries – Iteration
Section A: Quick Questions
Q1
A — Each key, one at a time
Q2
A — for v in d.values():
Q3
A — for k, v in d.items():
Q4
B — a then b
Q5
B — x 10
Section B: Match the Pairs
Q1
d = {'a':1} for k, v in d: print(k, v) → Error: Must use d.items()
Q2
d = {'x':1} d['x'] += 1 del d['x'] print(d['x']) → Error: Key deleted, KeyError
Q3
for k, v in d.items(): print(k, v) → Valid: Unpack key and value
Q4
if 'name' in d: print(d['name']) → Ok: Check key before accessing
Q5
d = {'a':1,'b':2} for k in d: print(k) → Output: a b
Q6
d = {'x':10} for k,v in d.items(): print(k,v) → Output: x 10
Section C: Definitions
Q1
for key in d: — Iterate through all keys in the dictionary.~~for v in d.values():
Section D: Fill in the Blanks
Q1
for key in d: print(key)
Q2
for val in d.values(): print(val)
Q3
for k, v in d.items(): print(k, v)
Q4
if 'name' in d: print('Found')
Q5
for key in d: print(d[key])
Section E: Spot the Error
Q1
B — Looping over d directly gives only keys. Use .items() for key-value pairs.
Q2
B — for val in d: gives KEYS, not values. Use .values() for values.
Q3
B — .keys() only gives keys. Use .items() to get both key and value.
Q4
B — Without unpacking, k is a tuple like ('a', 1). Use k, v to unpack.
Q5
A — 'in' checks KEYS by default. To check values, use d.values().