Python Counter: How to Use + 5 Examples

Clint Fontanella
Clint Fontanella


If you're a web developer looking to take your programming skills to the next level, there's no doubt you're familiar with Python's most essential functions, libraries, and tools. 

web developer using python counter

But, there’s one tool that you might not have explored yet that can add tremendous value to your coding arsenal: the Python Counter.

This module quickly and efficiently counts occurrences of items in a list or other iterable object. And, in this post, we’re going to take a deep dive into the Python Counter, exploring how it works and how you can use it to streamline your code. 

So, whether you're a seasoned pro or a relatively new web developer, read on to discover how you can leverage the Counter library to enhance your Python programming skills.Download Now: An Introduction to Python [Free Guide]

What is Counter in Python?

Counter is a Python library that counts the frequency of elements in a sequence. It creates a dictionary-like object that calculates the occurrences of every element in a list, tuple, or string. 

what is counter in python

Here’s how you would import the Counter library.


from collections import Counter


The Counter object comes equipped with several built-in methods that help users identify how many elements are present in a string or combine several counter objects together. This function is a powerful tool in Python for data analysts and coders across a range of industries, including finance, retail, marketing, and more.

HubSpot Video

Now that you are familiar with the Counter library in Python, let’s put it to use with an interactive example.

How to Use Counter in Python

First, let’s import the collections library by entering the following code at the top of your Python script:


from collections import Counter

Next, create a counter object by passing a sequence of elements to the counter function. The sequence can be a list, a tuple, or even a string. You can also create an empty counter object and add elements to it later using the update() method.


my_list = [1, 1, 2, 3, 3, 3, 4, 4, 4, 4]

counter_obj = Counter(my_list)



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

This output shows a dictionary-like object where each unique element in the sequence is stored as the key and its count is stored as the value. You can play with this function in the interactive module below.

Let’s look at a few more examples of the counter object in Python.

Python Counter Examples

The following examples demonstrate the various methods you can use with the Counter library. You can use the interactive code module above to test each one, like in the example below. 

Python counter example

Counting Objects

One of the most common uses of the counter library is to count the number of objects in a given string or dataset. For this task, we can use the following code:


from collections import Counter

text_data = "This is an example of text data. It contains multiple words and sentences."

words = text_data.split()

word_count = Counter(words)

top_words = word_count.most_common(3)



[('This', 1), ('is', 1), ('an', 1)]

Counting Most Common Elements

You can also use counter with several other methods. For example, the most_common() method returns a list of the most common elements and their counts, in descending order. 

To get the two most common elements in the counter object, you can use the following code:


from collections import Counter

my_list = [1, 1, 2, 3, 3, 3, 4, 4, 4, 4]

counter_obj = Counter(my_list)



[(4, 4), (3, 3)]

Looping Through Elements

The elements() method returns an iterator over the elements in the sequence. You can use it to loop through the elements and their counts:


from collections import Counter

my_list = [1, 1, 2, 3, 3, 3, 4, 4, 4, 4]

counter_obj = Counter(my_list)

for element, count in counter_obj.items():

    print(f"{element}: {count}")


1: 2

2: 1

3: 3

4: 4

Updating Objects

The update() method allows you to merge additional elements into the Counter object. For example:


from collections import Counter

new_list = [4, 5, 5, 5]




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

Subtracting Objects

The subtract() method allows you to deduct elements from a Counter object. For example, this code substracts the values of z2 from the values of z1 :


from collections import Counter

z1 = Counter(a=3, b=2, c=1)

z2 = Counter(a=1, b=2, c=3)




Counter({'a': 2, 'b': 0, 'c': -2})

Using Counter in Python

The Counter module is a powerful and versatile tool that can dramatically simplify your Python coding, allowing you to count the occurrences of items in a list or other iterable object with greater ease and accuracy. From creating custom histogram charts to finding the most common words in a text file, the Python Counter library has a wide range of practical applications for developers of all levels.

Author's Note: This post was written/edited by a human with the assistance of generative AI.  python

Topics: What Is Python?

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

A guide for marketers, developers, and data analysts.