Dict Detective — Dictionaries & sets
Six case files, worked with keys, values and sets.
How to play
Each level is a small investigation. Build or change a dictionary and press Run; its entries
appear as key → value cards. The two set levels swap the cards for three regions — what only the
first witness said, what both said, what only the second said — so an intersection stops being an abstraction.
What each level teaches
- Open the case file — building a dictionary and reading a key.
- Lookups that cannot crash —
.get()with a default, andinfor testing keys. - Update the file — adding, replacing and deleting entries.
- Count the sightings — the counting idiom, the most reused dictionary pattern there is.
- Two witness lists — set intersection, union and difference.
- Remove the duplicates —
set()to dedupe,sorted()to show the result.
The one line worth memorising
counts[w] = counts.get(w, 0) + 1
That is the whole counting pattern. Without .get() you have to check whether the key exists first,
because counts[w] += 1 raises KeyError the first time it sees a word. Level 4 bans
collections.Counter deliberately — Counter is the right tool in real code, but you should
be able to write the four lines it replaces before you reach for it.
When to use a set instead of a list
Three reasons, all of them visible in level 6:
- Duplicates vanish automatically, which is the shortest way to deduplicate anything.
- Membership is fast.
x in some_listscans every item;x in some_setdoes not, which starts to matter once the collection is large. - Overlap questions become one operator instead of a nested loop.
The cost is that a set has no order and cannot hold unhashable things such as lists. Sort it when you need to show it. More in the tutorial on Python dictionaries and sets.
Frequently asked questions
What is the difference between dict[key] and dict.get(key)?
dict[key] raises KeyError if the key is missing. dict.get(key) returns None instead, and dict.get(key, default) returns whatever default you pass — which is what makes the counting pattern work.
How do I count items in a list with a dictionary?
Start with an empty dict and loop: counts[item] = counts.get(item, 0) + 1. collections.Counter does the same job in one line, but this game asks you to write it out first.
What is the difference between a list and a set in Python?
A list keeps order and allows duplicates; a set does neither, but it removes duplicates for free, tests membership much faster, and supports intersection, union and difference directly.
How do I remove duplicates from a list in Python?
set(items) removes them. For a list back in a predictable order use sorted(set(items)); to keep the original order instead, use list(dict.fromkeys(items)).
Can a dictionary key be anything?
It has to be hashable, which in practice means strings, numbers and tuples are fine, while lists, sets and dicts cannot be keys.
Keep going
- Read the dictionaries & sets tutorial — the written explanation behind every level here.
- Variable Vault — variables & data types.
- Operator Arcade — numbers & operators.
- String Surgeon — strings & slicing.
- 20 Python practice exercises with worked solutions.
Take the games with you
Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.
Get it on Google Play