Count repeated labels
Quick review (2 minutes)
A tally sheet for a vote has one line for each answer.
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.
Set
counts = {}above the loop.Loop over the labels with a for loop.
Inside the loop, test
if label in counts:.When it is true, write
counts[label] += 1; otherwise writecounts[label] = 1, which stores the new key.After the loop, read a count with
counts[label]or print the whole dictionary.
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
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.
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.
- Python Software Foundation, Python 3.13 Language Reference, 8.1 The if statement
- Python Software Foundation, Python 3.13 Library Reference, Built-in Exceptions: KeyError
- Python Software Foundation, Python 3.13 Language Reference, 7.2.1 Augmented assignment statements
- Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries
- Python Software Foundation, Python 3.13 Tutorial, 5.1 More on Lists
- Python Software Foundation, Python 3.13 Tutorial, 4.2 for Statements
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
- Set
counts = {}above the loop. - Loop over the labels with a for loop.
- Inside the loop, test
if label in counts:. - When it is true, write
counts[label] += 1; otherwise writecounts[label] = 1, which stores the new key. - After the loop, read a count with
counts[label]or print the whole dictionary.
- Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries
- Python Software Foundation, Python 3.13 Tutorial, 4.2 for Statements
- Python Software Foundation, Python 3.13 Language Reference, 8.1 The if statement
- Python Software Foundation, Python 3.13 Language Reference, 7.2.1 Augmented assignment statements
6 Worked example
Count five votes
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
| vote | In counts? | counts after the pass | Explanation |
|---|---|---|---|
| tea | no | tea 1 | A new label is stored with 1. |
| milk | no | tea 1, milk 1 | Another new label. |
| tea | yes | tea 2, milk 1 | tea is a key, so 1 is added. |
| water | no | tea 2, milk 1, water 1 | A third label is stored with 1. |
| tea | yes | tea 3, milk 1, water 1 | tea 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.
- Python Software Foundation, Python 3.13 Library Reference, Built-in Exceptions: KeyError
- Python Software Foundation, Python 3.13 Language Reference, 7.2.1 Augmented assignment statements
- Python Software Foundation, Python 3.13 Language Reference, 8.1 The if statement
- Python Software Foundation, Python 3.13 Tutorial, 5.5 Dictionaries
- Python Software Foundation, Python 3.13 Tutorial, 4.2 for Statements
Quick checks
Quick check 1 of 2
Predict what this program displays. The program reads no input.
counts = {}
for ch in "abba":
if ch in counts:
counts[ch] += 1
else:
counts[ch] = 1
print(counts)Show the steps and the 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
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
- Python 3.13 Tutorial, 5.5 Dictionaries. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Library Reference, Built-in Exceptions: KeyError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 7.2.1 Augmented assignment statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Tutorial, 5.1 More on Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Tutorial, 4.2 for Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.