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 returns a Counter object that is a subclass of Python’s dictionary. We then run the same command but assign it ...