← Python

Dictionaries – Advanced (comprehensions, merging)

A dict comprehension builds a dictionary in one expression; the | operator merges two dicts

What it does: .items() yields (key, value) pairs for each entry in the dict. The pipe | merges dicts - if both have the same key, the right side value wins. zip pairs the two lists, dict() converts those pairs into a dictionary. Add 'if condition' before the closing brace to filter - only pairs where condition is True get included. Unpacking k, v then using v: k reverses which side is the key and which is the value. The right dict overwrites matching keys - think of it as d1 updated with d2. zip(['a','b'], [1,2]) gives ('a',1) then ('b',2) - like a zipper joining two lists.

Common mistakes: Dict comprehensions use curly braces {}, not square brackets []. Dicts don't support +. Use | or {**d1, **d2} to merge.

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
{x: x**2 for x in range(4)} creates?
{0:0, 1:1, 2:4, 3:9}[0, 1, 4, 9]
Matches
❌ d1 + d2
Error: Use d1 | d2 or {**d1, **d2} to merge
Applying
Meanings
Term
Dict comprehension
Meaning
{key: value for item in iterable} builds a dictionary concisely.
Practice
squares = {x: x**2 for x in range(6)}
Testing
Gaps
squares = {x: x**2 x in range(5)}
Check Answer
Spots
1# Build a squares dictionary
2squares = [x: x**2 for x in range(5)]

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

The 'Camel' badge — Endurance Champion, 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. {x: x**2 for x in range(4)} creates?
A. {0:0, 1:1, 2:4, 3:9} ✓
B. [0, 1, 4, 9]
Dict comprehensions use key: value pairs - x maps to x squared for each x
2. d1 | d2 does what?
A. Checks if equal
B. Merges both dicts (d2 wins conflicts) ✓
The pipe | merges dicts - if both have the same key, the right side value wins
3. {**d1, **d2} what does ** do?
A. Unpacks dict entries into a new dict ✓
B. Squares the keys
** spreads all key-value pairs from a dict into the surrounding context
4. dict(zip(['a','b'], [1,2])) creates?
A. [('a',1), ('b',2)]
B. {'a': 1, 'b': 2} ✓
zip pairs the two lists, dict() converts those pairs into a dictionary
5. Can dict comprehensions have conditions?
A. Yes add if at the end ✓
B. No
Add 'if condition' before the closing brace to filter - only pairs where condition is True get included

Match the Pairs

d1 + d2 Error: Use d1 | d2 or {**d1, **d2} to merge
{k for k, v in d} Error: This is a set, not a dict comprehension
{k: v for k, v in d.items()} Valid: Dictionary comprehension
{**d1, **d2} Ok: Merge two dicts using unpacking
{x: x**2 for x in range(4)} Output: {0: 0, 1: 1, 2: 4, 3: 9}
dict(zip(['a','b'], [1,2])) Output: {'a': 1, 'b': 2}

Key Term

Dict comprehension
{key: value for item in iterable} builds a dictionary concisely.~~Merge operator

Fill in the Blanks

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

Spot the 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')
Line B — Dict comprehensions use curly braces {}, not square brackets []
Question 2
A d1 = {'a': 1}
B d2 = {'b': 2}
C merged = d1 + d2
D print('Merged:', merged)
Line C — Dicts don't support +. Use | or {**d1, **d2} to merge.
Question 3
A names = ['Alice', 'Bob']
B scores = [90, 85]
C result = zip(names, scores)
D print(result)
Line D — zip returns a zip object. Wrap in dict() to get a dictionary.
Question 4
A d = {'a': 1, 'b': 2}
B new = {k: v for k in d}
C print('New:', new)
D print('Done')
Line B — Need .items() to get both key and value and unpack as k, v
Question 5
A d1 = {'a': 1}
B d2 = {'a': 2}
C merged = d1 | d2
D print(merged['a']) # expects 1
Line D — In d1 | d2, d2 wins for shared keys. merged['a'] is 2, not 1.
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?