BeginnerCoder

Lesson 9 — Counter

Count in one line

collections.Counter is a dict that counts things for you. Pass it any sequence — a list, or a string of letters — and it returns each item with how many times it appears. most_common(k) gives the k most frequent items.

from collections import Counter
print(Counter(["a", "b", "a"]))  # Counter({'a': 2, 'b': 1})

Why it matters: counting votes, words or errors is a common task, and Counter replaces a whole loop.

Want it another way?
Task

Count the letters in "mississippi" and print the Counter.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.