In this tutorial, you will learn how to use counter objects from Python’s collections module.
When working with long sequences in Python (such as Python lists or strings), you may need to store which items appear in the sequence and how many times they occur.
Python dictionaries are built-in data structures suitable for such applications. However, Python’s Counter class in the collection module can simplify this by constructing a counter that is a dictionary of items and the number of items in the sequence.
In the next few minutes, you’ll learn:
- Using Python’s counter object
- Create a Python dictionary to store item count values in an iterable object
- Rewrite a dictionary with a simplified syntax using Python counters.
- Perform operations such as updating or subtracting elements or finding intersections between two counter objects.
- Use
most_common()method to get the most frequently used items in the counter.
Let’s get started!
Python collection module and counter class
Python dictionaries are often used to iterably store items and their counts. Items and numbers are stored as keys and values respectively.
Counter class is part of Python’s built-in collections module, so you can import it into your Python script like this:
from collections import CounterAfter importing the Counter class as shown above, you can instantiate a counter object as follows:
<counter_object> = Counter(iterable)here:
-
iterableAny valid Python iterable object, such as a Python list, string, or tuple. - Items in iterable must be hashable .
Now that you know how to create a counter object from a Python iterable using Counter , let’s start coding.
The example used in this tutorial can be found in this GitHub gist .

How to create a Counter object from Python Iterables
For example, let’s create a Python string called ‘renaissance’ and name it word .
>>> word = "renaissance" Our goal is to create a dictionary where each character in word string is mapped to the number of times it appears in the string. One approach is to use a for loop, like this:
>>> letter_count = {}
>>> for letter in word:
... if letter not in letter_count:
... letter_count[letter] = 0
... letter_count[letter] += 1
...
>>> letter_count
{'r': 1, 'e': 2, 'n': 2, 'a': 2, 'i': 1, 's': 2, 'c': 1}Let’s analyze what the above code snippet does.
- Initialize
letter_countto an empty Python dictionary. - Loop over
wordcolumn. - Check if
letterletter_countexists in the dictionary. - If
letterdoes not exist, add the letter with value0, then increment the value by 1. - Each time
letterappears inword, the value corresponding toletteris incremented by 1. - This continues until it loops through the entire string.
I built the letter_count dictionary on my own by looping through the string word using a for loop.
Next, let’s use the collection module’s Counter class. You can simply pass word string to Counter() to get letter_count without looping through the iterable.
>>> from collections import Counter
>>> letter_count = Counter(word)
>>> letter_count
Counter({'e': 2, 'n': 2, 'a': 2, 's': 2, 'r': 1, 'i': 1, 'c': 1}) Counter objects are also Python dictionaries. You can check this using the built-in isinstance() function.
>>> isinstance(letter_count,dict)
True As you can see, isinstance(letter_count, dict) returns True , indicating that the counter object letter_count is an instance of the Python dict class.
.jpg)
Modifying a counter object
So far, you have learned how to create a counter object from a Python string.
You can also modify a counter object by updating it with elements from another iterable or subtracting another iterable from it.
Updating a counter from another iterable element
Let’s initialize another string another_word .
>>> another_word = "effervescence" Suppose you want to update letter_count counter object with another_word string item.
You can use update() method on the counter object letter_count .
>>> letter_count.update(another_word)
>>> letter_count
Counter({'e': 7, 'n': 3, 's': 3, 'c': 3, 'r': 2, 'a': 2, 'f': 2, 'i': 1, 'v': 1}) In the output, you can see that the counter object has been updated to also include the characters of another_word and their occurrences.
Subtract elements from another iterable
Next, let’s subtract the value of another_word from letter_count object. To do this, use subtract() method. <counter-object>.subtract(<some-iterable>) subtracts the values corresponding to the items in <some-iterable> from <counter-object> .
Let’s subtract another_word from letter_count .
>>> letter_count.subtract(another_word)
>>> letter_count
Counter({'e': 2, 'n': 2, 'a': 2, 's': 2, 'r': 1, 'i': 1, 'c': 1, 'f': 0, 'v': 0}) You can see that the values corresponding to the characters in another_word are subtracted, but the added keys “f” and “v” are not removed. These are now mapped to the value 0.
Note : Here we are passing
another_wordto thesubtract()method call, which is a Python string. You can also pass a Python counter object or another iterable.

Intersection between two counter objects in Python

You may want to find the intersection between two Python counter objects to determine the keys that are common between the two.
Let’s create a counter object, say letter_count_2 , from another_word string ‘effervescent’.
>>> another_word = "effervescence"
>>> letter_count_2 = Counter(another_word)
>>> letter_count_2
Counter({'e': 5, 'f': 2, 'c': 2, 'r': 1, 'v': 1, 's': 1, 'n': 1}) You can find the intersection between letter_count and letter_count_2 using the simple & operator.
>>> letter_count & letter_count_2
Counter({'e': 2, 'r': 1, 'n': 1, 's': 1, 'c': 1})Notice how the key is obtained and how many occurrences the two words have in common . Both “renaissance” and “bubbly” have two occurrences of “e” and one occurrence each of “r,” “n,” “s,” and “c.”
Find the most frequently used items using most_common
Another common operation on Python counter objects is finding the most frequently occurring items.
To get the top k most common items in a counter, use most_common() method on the counter object. Here we call most_common() on letter_count to find the three most frequently occurring letters.
>>> letter_count.most_common(3)
[('e', 2), ('n', 2), ('a', 2)]In the word “renaissance” you can see that the letters “e”, “n”, and “a” occur twice.
This is especially useful if your counter contains a large number of entries and you are interested in working with the most common keys.
conclusion
A quick review of what you learned in the tutorial.
- You can use
Counterclass in Python’s built-in collections module to obtain a dictionary of count values for all items in an iterable. You need to ensure that all items in the iterable are hashable. - You can update the contents of one Python counter object with the contents of another counter object or other iterable object using the
update()method with the syntaxcounter1.update(counter2). Note that you can use any iterable in place ofcounter2. - If you want to remove the contents of one of your iterables from the updated counter, you can use
subtract()method:counter1.subtract(counter2). - To find common elements between two counter objects, you can use the & operator. Given two counters
counter1andcounter2,counter1 & counter2returns the intersection of these two counter objects. - To get the k most frequently used items in a counter, you can use
most_common()method.counter.most_common(k)Shows the k most common items and the number of each.
Next, you’ll learn how to use another class in the collection module: the default dictionary. To handle missing keys, you can use the default dictionary instead of the regular Python dictionary.




![How to set up a Raspberry Pi web server in 2021 [Guide]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)











































