DSCI 221, 2025 W2
February 11, 2026
Dictionaries are Python’s key-value lookup structure.
Lists: Access by position (index)
Dictionaries: Access by meaningful key
Dictionaries are also fast — We hope for O(1)…
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 when: Finding common elements, detecting duplicates
Use case: Count how many times each item appears.
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!
Open the Data Heist activity. You’ll solve 4 puzzles with LARGE datasets.
Inspired by Advent of Code puzzles!
| 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) |