Practice Bank: Dictionaries & Sets
A deeper set of practice problems, organized by topic, for extra reps
beyond the main exercises — including scenario-based problems
written in the same style you'll meet in real interviews. Uses
everything from Chapters 1-7 plus this chapter's own toolkit:
dictionary literals, indexing/KeyError,
.get() with a default, in for key
existence, dict methods from the lesson, looping over dictionaries,
sets and set operations, and nested dictionaries. No comprehensions,
lambda, functions (def),
try/except, file I/O, classes, or imports
yet.
starter.py below, then run it from a terminal
with python3 starter.py.
Topic 1: Creating Dictionaries & Accessing Values
1 Print a dict and access values
Find # TODO 1.1. Given student = {"name": "Asha", "age": 21, "major": "CS"}, print the whole dict, then print student["name"] and student["age"].
2 KeyError on a missing key
Find # TODO 1.2. Given prices = {"apple": 1.50, "banana": 0.75}, explain in a comment why prices["mango"] raises a KeyError.
3 Safe lookups with .get()
Find # TODO 1.3. Use .get("mango", 0) and .get("apple", 0) on prices and print both results.
4 Key existence with in
Find # TODO 1.4. Given inventory = {"pens": 40, "notebooks": 15}, check whether "pens" and "erasers" are keys using in.
5 Debug the Code
Find # TODO 1.5. This raises a KeyError looking up "kiwi". Fix it with .get() and a default of 0.
Employee lookup
Find # TODO 1.A. Print an employee's name and department, then safely look up a "manager" field that doesn't exist with .get().
Why .get() over square brackets?
Find # TODO 1.B. Show the safe .get() version of a missing-key lookup and explain in a comment why it's preferred for optional fields.
Topic 2: Dict Methods That Add, Update & Remove
1 update()
Find # TODO 2.1. Given profile = {"name": "Meera", "age": 30}, use .update() to change "age" and add "city" in one call.
2 pop()
Find # TODO 2.2. Given a shopping cart dict, use .pop() to remove "eggs" and print the popped value plus the remaining cart.
3 setdefault()
Find # TODO 2.3. Use .setdefault() on a missing key and on an existing key, and print both results to see the difference.
4 Debug the Code
Find # TODO 2.4. This crashes with a KeyError because .pop() has no default. Fix it by passing one.
5 keys(), values(), items(), del
Find # TODO 2.5. Print .keys(), .values(), and .items() as lists, then remove a key with del.
Shopping cart update
Find # TODO 2.A. Use .update() and .pop() together to edit quantities in a cart.
.get() vs. .setdefault()
Find # TODO 2.B. Show that .setdefault() actually inserts a missing key while .get() never modifies the dict, and explain why in a comment.
Topic 3: Looping Over Dictionaries
1 Looping over keys
Find # TODO 3.1. Use for name in ages to print each key, showing plain iteration visits keys only.
2 Looping with .items()
Find # TODO 3.2. Use for name, age in ages.items() to print an f-string line per person.
3 Insertion order
Find # TODO 3.3. Add keys one at a time to an empty dict, then loop over it to confirm insertion order is preserved.
4 Debug the Code
Find # TODO 3.4. This treats plain dict iteration as if it gave (key, value) pairs. Fix it by looping over .items().
5 Accumulate over .items()
Find # TODO 3.5. Loop over prices.items() with an accumulator to total up every price.
Attendance sheet
Find # TODO 3.A. Print each person's status, then count how many are "Present" with an accumulator.
What order do dict loops visit?
Find # TODO 3.B. Loop over an ordered set of steps and explain in a comment that Python dicts preserve insertion order since 3.7.
Topic 4: Sets — Unique Collections & Set Operations
1 Creating sets
Find # TODO 4.1. Build a set from a list with duplicates using set(), and create an empty set with set() (not {}).
2 add() and remove()
Find # TODO 4.2. Use .add() to add a tag and .remove() to remove one from a set.
3 Union and intersection
Find # TODO 4.3. Compute team_a | team_b and team_a & team_b for two sets of names.
4 Debug the Code
Find # TODO 4.4. This raises a KeyError from .remove() on a value not in the set. Fix it by checking in first.
5 Set difference
Find # TODO 4.5. Use all_students - passed_students to find students who didn't pass.
Tag deduplication
Find # TODO 4.A. Build a set from a raw list of repeated tags to automatically deduplicate them.
Why sets for membership checks?
Find # TODO 4.B. Check membership on a set of IDs and explain in a comment why sets beat lists for large membership checks.
Topic 5: Nested Dictionaries & Real-World Records
1 Double indexing
Find # TODO 5.1. Given a dict of dicts, print a nested value with students["Asha"]["age"].
2 Mutating a nested value
Find # TODO 5.2. Change a nested value with students["Asha"]["major"] = "Data Science" and print the result.
3 Looping over nested dicts
Find # TODO 5.3. Loop over an inventory dict of dicts with .items() unpacking and print a formatted line per item.
4 Debug the Code
Find # TODO 5.4. This indexes the outer and inner keys in the wrong order. Fix it to look up the name first, then the field.
5 Dict of lists
Find # TODO 5.5. Given a dict mapping departments to lists of employees, loop over it and print each team's size with len().
Customer order record
Find # TODO 5.A. Print an order's customer and total, and loop over its list of items.
Modeling a user with many phone numbers
Find # TODO 5.B. Combine a dict with a list-valued field and explain in a comment why that pattern models real-world records before classes.
Topic 6: Real-World Dicts & Sets in Production
1 Word-frequency counter
Find # TODO 6.1. Build a frequency dictionary from a list of words using .get(word, 0) + 1.
2 Deduplicating with a set
Find # TODO 6.2. Deduplicate a list of emails with set() and compare lengths before and after.
3 A lookup table
Find # TODO 6.3. Use a dict as an ID-to-name lookup table, with .get() handling a missing ID.
4 Debug the Code
Find # TODO 6.4. This raises a KeyError the first time a new grade letter appears. Fix it using .get().
5 Comparing two teams' emails
Find # TODO 6.5. Use set intersection and difference to compare two teams' email sets.
Log line word frequency
Find # TODO 6.A. Build and print a frequency table from a list of words pulled from a server log.
Counting unique visitors
Find # TODO 6.B. Find the number of unique visitor IDs using a set's length, and explain in a comment why a set is the right tool.
solution.py. Your exact
wording doesn't need to match — the goal is that your program
runs without errors and does what each TODO asks.