Coding Python
Home›Python Coding Games›Dict Detective

Dict Detective — Dictionaries & sets

Six case files, worked with keys, values and sets.

6 levels · Beginner → Intermediate
Level 1

Checks
    Your PythonPython 3
    1

    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

    1. Open the case file — building a dictionary and reading a key.
    2. Lookups that cannot crash — .get() with a default, and in for testing keys.
    3. Update the file — adding, replacing and deleting entries.
    4. Count the sightings — the counting idiom, the most reused dictionary pattern there is.
    5. Two witness lists — set intersection, union and difference.
    6. 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_list scans every item; x in some_set does 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.

    Practise dictionaries & sets on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    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

    ← All Python coding games  Read the tutorial →

    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
    Coding Python app icon