Quarry School

Count repeated labels

Subject: Computing
Level 1 · FoundationsAbout 4 min read1 new term
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    A tally sheet for a vote has one line for each answer.

    Rule

    To count labels, start with an empty dictionary. For each label, add 1 to its value when it is already a key, and store it with 1 when it is not. After the loop, each key's value is the number of times that label appeared.

    Step

    1. Set counts = {} above the loop.

    2. Loop over the labels with a for loop.

    3. Inside the loop, test if label in counts:.

    4. When it is true, write counts[label] += 1; otherwise write counts[label] = 1, which stores the new key.

    5. After the loop, read a count with counts[label] or print the whole dictionary.

    Trap

    Writing only counts[vote] += 1 stops at the first vote with a KeyError, because the count is read before anything was stored for that key. The else branch stores the first 1.

    Setting counts = {} inside the loop empties the tally on every pass, and only the last vote would be left. Put it above the loop.

    1 In plain words

    A tally sheet for a vote has one line for each answer. Each new ballot either adds a mark to a line already there or starts a new line.

    A dictionary makes the same tally: each label is a key, and its value is how many times it has appeared. Go through the labels with a for loop. If a label is already a key, add 1 to its count; otherwise store it with the count 1.

    Remember one sentence: add 1 for a label you have seen, store 1 for a new one. To look up the dictionary operations, see section 5.5 of the Python 3.13 tutorial.

    2 Picture it

    VoteAlready a key?counts afterteanotea 1milknotea 1, milk 1teayestea 2, milk 1

    A table of the first three passes of the vote count. The first vote, tea, is not a key, so tea is stored with 1. Milk is not a key either, so milk is stored with 1. The next tea is already a key, so its count becomes 2. The third row is marked.

    The first three passes of the vote count

    3 Why

    The if decides between two different steps. counts[vote] += 1 reads the old count, so it works only for a label already stored; for a new label that read would raise a KeyError. So a new label is stored with 1 instead.

    The counts start as an empty dictionary, {}, before the loop, like any running total.

    When the labels are in a list and only one label's count is needed, votes.count("tea") gives it directly. The dictionary tally gives every label's count in one pass of the loop.

    4 The rule

    To count labels, start with an empty dictionary.

    For each label, add 1 to its value when it is already a key, and store it with 1 when it is not. After the loop, each key's value is the number of times that label appeared.

    5 How

    1. Set counts = {} above the loop.
    2. Loop over the labels with a for loop.
    3. Inside the loop, test if label in counts:.
    4. When it is true, write counts[label] += 1; otherwise write counts[label] = 1, which stores the new key.
    5. After the loop, read a count with counts[label] or print the whole dictionary.

    6 Worked example

    Count five votes

    Python code
    votes = ["tea", "milk", "tea", "water", "tea"]
    counts = {}
    for vote in votes:
        if vote in counts:
            counts[vote] += 1
        else:
            counts[vote] = 1
    print(counts)
    print(counts["tea"])
    

    Standard input

    Empty

    Standard output

    {'tea': 3, 'milk': 1, 'water': 1}
    3
    

    Predict the two output lines first, then run the file. tea is counted three times; milk and water once each, and the keys appear in the order they were first stored.

    Trace

    Trace the vote count
    voteIn counts?counts after the passExplanation
    teanotea 1A new label is stored with 1.
    milknotea 1, milk 1Another new label.
    teayestea 2, milk 1tea is a key, so 1 is added.
    waternotea 2, milk 1, water 1A third label is stored with 1.
    teayestea 3, milk 1, water 1tea reaches 3.

    7 The trap

    Writing only counts[vote] += 1 stops at the first vote with a KeyError, because the count is read before anything was stored for that key. The else branch stores the first 1.

    Setting counts = {} inside the loop empties the tally on every pass, and only the last vote would be left. Put it above the loop.

    Quick checks

    Quick check 1 of 2

    Predict what this program displays. The program reads no input.

    Python code
    counts = {}
    for ch in "abba":
        if ch in counts:
            counts[ch] += 1
        else:
            counts[ch] = 1
    print(counts)
    Show the steps and the answer
    Answer

    It displays {'a': 2, 'b': 2}.

    Why: a is stored with 1, b is stored with 1, then b and a each get 1 more. The keys keep the order a, b.

    Source: Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries

    Quick check 2 of 2

    tags = ["red", "blue", "red", "red"]. What does print(tags.count("red")) display?

    Show the steps and the answer
    Answer

    It displays 3.

    Why: The list method count gives the number of times the value appears in the list.

    Source: Python Software Foundation, Python 3.13 Tutorial, 5.1 More on Lists

    Sources

    1. Python 3.13 Tutorial, 5.5 Dictionaries. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Library Reference, Built-in Exceptions: KeyError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    4. Python 3.13 Language Reference, 7.2.1 Augmented assignment statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    5. Python 3.13 Tutorial, 5.1 More on Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    6. Python 3.13 Tutorial, 4.2 for Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.