Skip to content

Python’s Counter: Count Items with Collections Counter

Python Counter Collections Count Items Cover Image

In this tutorial, you’ll learn how to use the Python Counter class from the collections module to count items. The Counter class provides an incredibly pythonic method to count items in lists, tuples, strings, and more. Because counting items is a common task in programming, being able to do this easily and elegantly is a useful skill for any Pythonista.

The Counter class provides a subclass to the Python dictionary, adding in many useful ways to easily count items in another object. For example, you can easily return the number of items, the most common item, and even undertake arithmetic on different Counter items.

By the end of this tutorial, you’ll have learned:

  • How to use the Counter class to count items in Python
  • How to get the most and least common items in a counter object
  • How to add and subtract different Counter objects
  • how to update Counter objects in Python
Video tutorial for Python Collections Counter

Understanding Python’s Collection Counter Class

The Python Counter class is an integral part of the collections module. The class provides incredibly intuitive and Pythonic methods to count items in an iterable, such as lists, tuples, or strings. This allows you to count the frequency of items within that iterable, including finding the most common item.

Let’s start by creating an empty Counter object. We first need to import the class from the collections module. Following that, we can instantiate the object:

# Creating an Empty Counter Object
from collections import Counter

counter = Counter()

Now that we have our first Counter object created, let’s explore some of the properties of the object. For example, we can check its type by using the type() function. We can also verify that the object is a subclass of the Python dictionary.

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter()

print('Type of counter is: ',type(counter))
print('Counter is a subclass of a dictionary: ', issubclass(Counter, dict))

# Returns:
# Type of counter is:  <class 'collections.Counter'>
# Counter is a subclass of a dictionary:  True

Now that you have an understanding of the Python Counter class, let’s get started with creating our first Counter object!

Creating a Counter Object in Python

Let’s create our first Python Counter object. We can pass in a string and the Counter object will return the counts of all the letters in that string.

The class takes only a single parameter, the item we want to count. Let’s see how we can use it:

# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter(a_string)

print(counter)

# Returns:
# Counter({'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1})

By printing out our counter, we’re able to see that it returns a dictionary-like object. The items are sorted by their frequency of each item in the object. In this case, we can see that the letter 'e' exists three times in our string.

Accessing Counter Values in Python

Because the Counter object returns a subclass of a dictionary, we can use dictionary methods to access the counts of an item in that dictionary. Let’s see how we can access the number of times the letter 'a' appears in our string:

# Accessing Counts in a Counter Object
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter(a_string)

print(counter['a'])

# Returns: 2

We can see that the letter 'a' exists twice in our string. We can even access the counts of items that don’t exist in our object.

# Counting Items that Don't Exist
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter(a_string)

print(counter['z'])

# Returns: 0

In a normal Python dictionary, this would raise a KeyError. However, the Counter class has been designed to prevent this by overriding the default behavior.

Finding the Most Common Item in a Python Counter

The Counter class makes it easy to find the most common item in a given object. This can be done by applying the .most_common() method onto the object. Let’s see how we can find the most common item in our object:

# Finding the Most Common Item
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter(a_string)

print(counter.most_common())

# Returns: [('e', 3), ('l', 3), ('o', 3), (' ', 3), ('t', 2), ('a', 2), ('h', 1), ('!', 1), ('w', 1), ('c', 1), ('m', 1), ('d', 1), ('g', 1), ('y', 1)]

We can see that this returns a list of tuples that’s been ordered by placing the most common items first. Because of this, we can access the most common item by accessing the first index:

# Accessing the Most Common Item
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter(a_string)

print(counter.most_common()[0])

# Returns: ('e', 3)

Finding the Least Common Item in a Python Counter

Similarly, we can access the least common item by getting the last index:

# Accessing the Least Common Item
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter(a_string)

print(counter.most_common()[-1])

# Returns: ('y', 1)

Finding n Most Common Items in a Python Counter

Te method also allows you to pass in an integer that returns just that number of items. Say we wanted to get the three most common items, you could write:

# Getting n Number of Most Common Items
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter(a_string)

print(counter.most_common(3))

# Returns: [('e', 3), ('l', 3), ('o', 3)]

Updating Counter Values in Python

One of the great things about the Python Counter is that values can also be updated. This can be done using the .update() method. The method accepts another iterable, which will update the values in place.

Let’s see how we can first count items using the collection’s Counter class and then pass in another iterable to update our counts.

# Updating Counter Values in Python
from collections import Counter

a_list = [1,2,3,1,2,3,1,1,2,1]
counter = Counter(a_list)
print(counter)

counter.update([1,2,3,4])
print(counter)

# Returns:
# Counter({1: 5, 2: 3, 3: 2})
# Counter({1: 6, 2: 4, 3: 3, 4: 1})

We can see that the values were updated in place, with the values of the new item.

Deleting Counter Values in Python

It’s also very easy to delete an item from a Counter object. This can be useful when you either want to reset a value or simple find a way to remove an item from being counted.

You can delete an item from a Counter object by using the del keyword. Let’s load a Counter object and then delete a value:

# Deleting an Item from a Counter Object
from collections import Counter

a_list = [1,2,3,1,2,3,1,1,2,1]
counter = Counter(a_list)
print(counter)

del counter[1]
print(counter)

# Returns:
# Counter({1: 5, 2: 3, 3: 2})
# Counter({2: 3, 3: 2})

Arithmetic Operations on Counter Objects in Python

It’s equally easy to apply arithmetic operations like addition and subtraction on Counter objects. This allows you to combine Counter objects or find the difference between two items.

This can be done using the + and the - operators respectively. Let’s take a look at addition first:

# Adding 2 Counter Objects Together
from collections import Counter

counter1 = Counter([1,1,2,3,4])
counter2 = Counter([1,2,3,4])

print(counter1 + counter2)

# Returns:
# Counter({1: 3, 2: 2, 3: 2, 4: 2})

Now let’s subtract the two counters:

# Subtracting 2 Counter Objects
from collections import Counter

counter1 = Counter([1,1,2,3,4])
counter2 = Counter([1,2,3,4])

print(counter1 - counter2)

# Returns:
# Counter({1: 1})

Combining Counter Objects in Python

We can also combine Counter objects using the & and | operators. These serve very different purposes. Let’s break them down a bit:

  • & will return the common positive minumum values
  • | will return the positive maximum values

Let’s take a look at the & operator first:

# Finding Common Minimum Elements
from collections import Counter

counter1 = Counter([1,1,2,2,2,3,3,4])
counter2 = Counter([1,2,3,4,5])

print(counter1 & counter2)

# Returns:
# Counter({1: 1, 2: 1, 3: 1, 4: 1})

Now let’s take a look at the maximums between the two Counter objects:

# Finding Maximum Elements
from collections import Counter

counter1 = Counter([1,1,2,2,2,3,3,4])
counter2 = Counter([1,2,3,4,5])

print(counter1 | counter2)

# Returns:
# Counter({2: 3, 1: 2, 3: 2, 4: 1, 5: 1})

Finding the Most Common Word in a Python String

Before closing out the tutorial, let’s take a look at a practical example. We can use Python’s Counter class to count find the most common word in a string. Let’s load the Zen of Python and find the most common word in that string.

Before we pass the string into the Counter class, we need to split it. We can use the .split() method to split at any white-space character, including newlines. Then we can apply the .most_common() method and access the first item’s value by accessing the [0][0] item:

# Finding the Most Frequent Word in a String
from collections import Counter

text = """
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""

counter = Counter(text.split())
print(counter.most_common()[0][0])

# Returns: is

Conclusion

In this post, you learned how to use the Python collection’s Counter class. You started off by learning how the class can be used to create frequencies of an iterable object. You then learned how to find the counts of a particular item and how to find the most and least frequent item. You then learned how to update counts, as well as perform arithmetic on these count items.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *