Dictionaries – Advanced (comprehensions, merging)

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

Dictionaries – Advanced (comprehensions, merging)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. {x: x**2 for x in range(4)} creates?
A {0:0, 1:1, 2:4, 3:9}
B [0, 1, 4, 9]
2. d1 | d2 does what?
A Checks if equal
B Merges both dicts (d2 wins conflicts)
3. {**d1, **d2} what does ** do?
A Squares the keys
B Unpacks dict entries into a new dict
4. dict(zip(['a','b'], [1,2])) creates?
A {'a': 1, 'b': 2}
B [('a',1), ('b',2)]
5. Can dict comprehensions have conditions?
A Yes add if at the end
B No

Match the Pairs

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

Left columnRight column
1d1 + d2 AValid: Dictionary comprehension
2{k for k, v in d} BOk: Merge two dicts using unpacking
3{k: v for k, v in d.items()} CError: This is a set, not a dict comprehension
4{**d1, **d2} DOutput: {'a': 1, 'b': 2}
5{x: x**2 for x in range(4)} EError: Use d1 | d2 or {**d1, **d2} to merge
6dict(zip(['a','b'], [1,2])) FOutput: {0: 0, 1: 1, 2: 4, 3: 9}

Definitions

Dict comprehension

Fill in the Blanks

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

1.
squares = {x: x**2 ___ x in range(5)}
2.
result = dict(___(names, scores))
3.
pos = {k: v for k, v in d.items() __ v > 0}
4.
merged = {____d1, **d2}
5.
inverted = {v: _ for k, v in d.items()}

Spot the Error

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

Question 1
A # Build a squares dictionary
B squares = [x: x**2 for x in range(5)]
C print('Squares:', squares)
D print('Done')
Question 2
A d1 = {'a': 1}
B d2 = {'b': 2}
C merged = d1 + d2
D print('Merged:', merged)
Question 3
A names = ['Alice', 'Bob']
B scores = [90, 85]
C result = zip(names, scores)
D print(result)
Question 4
A d = {'a': 1, 'b': 2}
B new = {k: v for k in d}
C print('New:', new)
D print('Done')
Question 5
A d1 = {'a': 1}
B d2 = {'a': 2}
C merged = d1 | d2
D print(merged['a']) # expects 1

Answer Key

Dictionaries – Advanced (comprehensions, merging)
Section A: Quick Questions
Q1
A — {0:0, 1:1, 2:4, 3:9}
Q2
B — Merges both dicts (d2 wins conflicts)
Q3
B — Unpacks dict entries into a new dict
Q4
A — {'a': 1, 'b': 2}
Q5
A — Yes add if at the end
Section B: Match the Pairs
Q1
d1 + d2 → Error: Use d1 | d2 or {**d1, **d2} to merge
Q2
{k for k, v in d} → Error: This is a set, not a dict comprehension
Q3
{k: v for k, v in d.items()} → Valid: Dictionary comprehension
Q4
{**d1, **d2} → Ok: Merge two dicts using unpacking
Q5
{x: x**2 for x in range(4)} → Output: {0: 0, 1: 1, 2: 4, 3: 9}
Q6
dict(zip(['a','b'], [1,2])) → Output: {'a': 1, 'b': 2}
Section C: Definitions
Q1
Dict comprehension — {key: value for item in iterable} builds a dictionary concisely.~~Merge operator
Section D: Fill in the Blanks
Q1
squares = {x: x**2 for x in range(5)}
Q2
result = dict(zip(names, scores))
Q3
pos = {k: v for k, v in d.items() if v > 0}
Q4
merged = {**d1, **d2}
Q5
inverted = {v: k for k, v in d.items()}
Section E: Spot the Error
Q1
B — Dict comprehensions use curly braces {}, not square brackets []
Q2
C — Dicts don't support +. Use | or {**d1, **d2} to merge.
Q3
D — zip returns a zip object. Wrap in dict() to get a dictionary.
Q4
B — Need .items() to get both key and value and unpack as k, v
Q5
D — In d1 | d2, d2 wins for shared keys. merged['a'] is 2, not 1.