CPSC 203, 2025 W2
February 12, 2026
Dictionaries are Python’s key-value lookup structure.
Lists: Access by position (index)
Dictionaries: Access by meaningful key
Dictionaries are also fast — requires the same time whether there are 100 entries, or 100,000.
Alternative to a data frame or named tuple:
Find everyone who was in Gallery A and Gallery B.
This works… but what if the lists are LONG?
With 10 people in Gallery A and 12 in Gallery B: 10 × 12 = 120 comparisons
With 10,000 in each: 100,000,000 comparisons!
Dictionary lookup is instant — it doesn’t matter how many keys are stored!
Use case: Count how many times each item appears.
Use case: Find two numbers that add up to a target.
Do you like this?
Use case: Find two numbers that add up to a target.
As we scan, we remember what we’ve seen — and check if the complement exists!
Use case: Organize items into categories
What if our items are dictionaries, not tuples?
Same pattern — just access the key with suspect["alibi"] instead of unpacking a tuple!
Use case: Convert values from one representation to another.
A Caesar cipher shifts by a fixed amount — we can decode with math:
But what if the spy used a random substitution? No formula can help!
The dictionary is the cipher — no formula, just lookup!
Open today’s activity. You’ll solve 2 new puzzles.
Dictionaries give you fast lookup by key — whether you have 100 items or 100,000.
| Pattern | Use Case | Technique |
|---|---|---|
| Record | Store named fields | {"name": ..., "age": ...} |
| Membership | Track what we’ve seen | seen[x] = True |
| Counting | Count occurrences | Counter(items) |
| Complement | Find pairs | Store & check complements |
| Grouping | Organize by category | defaultdict(list) |
| Translation | Convert values | Direct key → value mapping |
Reading Week — No Classes!
Enjoy the break, and keep working on Project 1.
See you in Week 8!