Count repeated values with a Python dictionary

← Back to Blog & Articles

A Python dictionary associates a key with a value. It can keep a running count for each distinct item in a collection.

Worked example

For the words cat, dog, cat, start with an empty dictionary. Reading cat creates a count of one for that key. Reading dog creates another key with one. Reading cat again increases its existing count to two. The final mapping records two cats and one dog without needing a separate variable for every possible word.

What to check

Check what should happen when a key has never appeared. The get method can provide a default count of zero before you add one. Also decide whether Cat and cat should count as the same word. Converting text to lowercase is a policy for the task, and the program should apply it consistently. Test repeated words, a single word, and an empty input so that the counting rule stays visible.

Continue with the book

Explore the full treatment, worked examples, and exercises in Code Quest with Python.


Companion article for Code Quest with Python, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top