Counter

Learn about the Counter object for counting items in the Python dictionary.

We'll cover the following

Overview

You’d think that algorithms could not get much simpler than using defaultdict(int). The I want to count specific instances in an iterable use case is common enough that the Python developers created a specific class for this exact purpose, simplifying things further. The previous code that counts characters in a string can easily be calculated in a single line:

Get hands-on with 1200+ tech skills courses.