← Data Structures
7 / 25
Lesson 7 — Frequency map
Counter: a frequency map in one line
Counting is so common that Python ships a ready-made tool: Counter from collections. Give it any sequence and it builds the key → count dict for you. .most_common(n) returns the n most frequent items as (item, count) pairs, biggest first.
from collections import Counter
Counter("banana") # {'a': 3, 'n': 2, 'b': 1}
Counter("banana").most_common(1) # [('a', 3)]
Why it matters: "top 5 products", "most common error" — one line instead of a loop.
Want it another way?
Task
Build counts with Counter and print the two most common letters. Expected: [('i', 4), ('s', 4)].
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.