Dictionaries & Data — Lesson 2: Count repeated events
New skill: Frequency tables and dictionary updates
Previously learned: Units 01–08
Why this matters
A classroom app could tally survey choices or event types without a fixed list of categories.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
events = ['run', 'jump', 'run']
counts = {}
for event in events:
counts[event] = counts.get(event, 0) + 1
print(counts)
Step 2 — Trace the code
- Start with an empty dictionary.
- For each event, get the current count (default 0).
- Add one and save the updated count under that event key.
Step 3 — Check your understanding
Trace the counts after each item in ['A','B','A'].
Step 4 — Watch for this mistake
Overwriting every count with 1 loses the previous total. Increment the existing count.
Practice: Count repeated events
Complete event_counts(events) to return a dictionary mapping each event string to how many times it occurred.
Function contract: event_counts(events)
Starter file: main.py
Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.
Hint 1 — Start
Create an empty dictionary for the counts.Hint 2 — Build
Loop over each event label.Hint 3 — Test
Assign `counts[event] = counts.get(event, 0) + 1`.Reflect
In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.
Optional reading
For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.