Modules in Python: Counter

Let's discover Counter and its functions.

Overview of Counter

The collections module also provides us with a neat little tool that supports convenient and fast tallies. This tool is called Counter.

We can run it against most iterables.

Press + to interact
from collections import Counter
print(Counter("superfluous"))
counter = Counter("superfluous")
print(counter["u"])

In this example, we import Counter from collections and then pass it a string (Line #2). This ...

Access this course and 1400+ top-rated courses and projects.