Counting and Grouping with a Python Dictionary

A dict can count things for you — one running total per key, all through one loop.

  • Lesson 5 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Tally and group

  1. The dragon: You've built running totals before, in a loop. Now you'll build MANY of them at once — one per key.

  2. The dragon: drops = ["gold", "gem", "gold", "sword", "gold"]. To count how many of each item dropped, start with an EMPTY dict.

    example.pyPython 3
    drops = ["gold", "gem", "gold", "sword", "gold"]counts = {}
  3. The dragon: For each item, check if it's already a key. If it is, add 1 to its count. If it isn't, this is the FIRST time — start it at 1.

    example.pyPython 3
    for item in drops:    if item in counts:        counts[item] = counts[item] + 1    else:        counts[item] = 1print(counts)
    Output{'gold': 3, 'gem': 1, 'sword': 1}
  4. The dragon: Here's the trap. Skip the if/else and just write counts[item] = counts[item] + 1 every time — it looks like the same idea, shorter.

  5. The dragon: The FIRST time an item shows up, counts[item] on the right side is reading a key that was never set — that's a real KeyError, same crash s3 taught, just hiding inside a loop this time.

    example.pyPython 3
    counts = {}for item in drops:    counts[item] = counts[item] + 1
    OutputKeyError: 'gold'
  6. The dragon: The elegant fix reuses .get() from segment 3: counts[item] = counts.get(item, 0) + 1. When the key is missing, .get() hands back 0 instead of crashing — so "0 + 1" becomes the key's first count, all in one line.

    example.pyPython 3
    counts = {}for item in drops:    counts[item] = counts.get(item, 0) + 1print(counts)
    Output{'gold': 3, 'gem': 1, 'sword': 1}
  7. The dragon: Same result either way — if/else spells out the two cases, .get() folds them into one. Both are real Python; pick whichever reads clearer to you.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    counts = {}. drops = ["gold", "gem"]. This code runs for EVERY item: counts[item] = counts[item] + 1 — no if/else, no .get(). What happens on the very first pass?

    question.pyPython 3
    counts = {}for item in drops:    counts[item] = counts[item] + 1
    • A KeyError — counts["gold"] is read before "gold" was ever setCorrect
    • It works fine — counts["gold"] just starts at 0 automatically
    • It silently skips that item and moves to the next one

    Why: A KeyError. counts[item] = counts[item] + 1 has to READ counts[item] first to add 1 to it — and the first time an item appears, that key doesn't exist yet. Either check "if item in counts" first, or use counts.get(item, 0) + 1 so the missing case has a safe default built in.

  2. Question 2 of 2

    counts = {}. drops = ["ice", "ice"]. Using counts[item] = counts.get(item, 0) + 1 each pass, what does counts["ice"] equal after BOTH passes?

    question.pyPython 3
    counts = {}for item in ["ice", "ice"]:    counts[item] = counts.get(item, 0) + 1print(counts["ice"])
    • 2 — the first pass starts it at 1, the second pass adds 1 moreCorrect
    • 1 — .get() resets the count back to its default every time it runs
    • 0 — .get() always returns the default, no matter what

    Why: 2. Pass one: "ice" isn't a key yet, so .get("ice", 0) returns 0, and counts["ice"] becomes 0 + 1 = 1. Pass two: "ice" IS a key now, so .get("ice", 0) returns the real 1, and counts["ice"] becomes 1 + 1 = 2.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    drops = ["gold", "gem", "gold"]. This code is trying to COUNT how many times each item drops — but it crashes with a KeyError the very first time it sees an item, because it reads counts[item] before that key has ever been set. Run it once to see the real error, then fix it so a brand-new key gets a safe starting value.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.