Python

How to Use the Counter Module in Python

This article will explain how to use the “Counter” module that comes by default with the Python programming language. All code samples in this article are tested with Python version 3.9.5 on Ubuntu 21.04.

About Counter Module

The Counter module, as the name suggests, can be used to count elements of an iterable or a hashable object in Python. Counter stores each element of an iterable (like a Python list object) as a Python dictionary key. Since Python dictionaries allow only unique keys, there is no repetition. The corresponding values for these dictionaries keys is the count or the amount of times an element appears in an iterable.

Basic Usage and Syntax

To understand the basic usage and syntax of Counter class, have a look at the code sample below:

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
print (counts)

The first statement imports the Counter module so that the Counter class can be used within the code. Next, a new Python list object is defined with some data. A new instance of Counter object is then created by passing “list1” as an argument. The final statement prints the output of the “counts” object.

After running the above code sample, you should get the following output:

Counter({'d': 4, 'b': 3, 'a': 2, 'e': 2, 'c': 1})

Note that the output returns a Counter type object and not a Python dictionary. Though it behaves like a Python dictionary with one minor difference which is explained below.

A Counter Object Behaves Like a Python Dictionary Object

A dictionary in Python is an object that stores items in “key:value” pairs. Below is an example of Python dictionary:

dict1 = {"a" : 1, "b" : 2}

The part before the “:” (colon) symbol is called “key” while the part after the colon symbol is called “value”. You can access value of any key in a Python dictionary by using the following syntax:

dict1 = {"a" : 1, "b" : 2}
print (dict1["a"])

You just have to supply the name of the key in “[ ]” (square) braces. If the key doesn’t exist in the dictionary, a “KeyError” is thrown.

The output of the Counter example above shows that when you create a new instance of the Counter class, a new Counter type object is returned. This Counter type object is nothing but a Python dictionary, except that it doesn’t throw a “KeyError” when a key value is missing. Instead, it assigns it a value of “0” (zero). You can also access the values of items in a Counter object by supplying key names in square braces, just like a dictionary object. Take a look at the code sample below:

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
print (counts["f"])

dict1 = {"a" : 1, "b" : 2}
print (dict1["c"])

After running the above code sample, you should get the following output:

0
Traceback (most recent call last):
  File "main.py", line 11, in
    print (dict1["c"])
KeyError: 'c'

As you can see in the output that when you access a key that doesn’t exist in a Counter object, “0” (zero) is returned. While on the other hand, a Python dictionary object throws a “KeyError” when a key is missing.

Manually Creating a Counter Object

There may be a case where you may want to manually define a Counter object instead of parsing an iterable like a Python list. To create a counter object, you can use the following syntax:

from collections import Counter

counter1 = Counter(a=4, b=3)
counter2 = Counter({"a" : 4, "b" : 3})
print (counter1)
print (counter2)

You can use argument style syntax shown in the first statement, or use Python dictionary style syntax shown in the second statement to create new instances of a Counter object. Both methods have the same effect and produce the same output.

After running the above code sample, you should get the following output:

Counter({'a': 4, 'b': 3})
Counter({'a': 4, 'b': 3})

Getting Most Common Items from a Counter Object

You can use the “most_common” method to get elements and their counts sorted in descending order from a Counter type object. Have a look at the code sample below:

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
print (counts.most_common())

The output returns a list of tuples and not a Counter or Python dictionary object.

You can also get only a few topmost elements by supplying a number to the “most_common” method as an argument.

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
print (counts.most_common(2))

After running the above code sample, you should get the following output:

[('d', 4), ('b', 3)]

Other Useful Counter Methods

You can access all keys and values of a Counter object using the “keys” and “values” methods respectively.

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
print (counts.keys())
print (counts.values())

After running the above code sample, you should get the following output:

dict_keys(['a', 'b', 'c', 'd', 'e'])
dict_values([2, 3, 1, 4, 2])

These methods produce iterable objects so you can loop through them.

You can get both keys and values by using the “items” method.

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
print (counts.items())

After running the above code sample, you should get the following output:

dict_items([('a', 2), ('b', 3), ('c', 1), ('d', 4), ('e', 2)])

You can also loop through the result obtained by using the “item” method.

You can convert a Counter object to a Python dictionary using the “dict” function.

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
print (dict(counts))

After running the above code sample, you should get the following output:

{'a': 2, 'b': 3, 'c': 1, 'd': 4, 'e': 2}

Looping through Keys and Values of a Counter Object

You can easily loop through key-value pairs of a Counter object using the “items” method explained above. Have a look at the code sample below:

from collections import Counter

list1 = ["a", "a", "b", "b", "b", "c", "d", "d", "d", "d", "e", "e"]
counts = Counter(list1)
for key, value in counts.items():
    print (key, value)

In the code, the key-pair variable can be accessed using “key” and “value” variables respectively in the “for” loop.

After running the above code sample, you should get the following output:

a 2
b 3
c 1
d 4
e 2

Conclusion

Python’s built-in “Counter” module provides a fast and efficient way to get count of items stored in an iterable type object. You can use the “most_common” method to get topmost pairs with highest counts by supplying your desired number as an argument.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.