How to use Counter in Python

Sonu Gupta
2 min readApr 29, 2021

--

  • Counter allows us to count the occurrences of a particular items.
  • It is collection of dict’s key and value are sorted.
  • Count can any integer value and zero and negetive values.

Let’s see how to create a Counter objects in Python.

from collections import Counter

language_counter = Counter() # empty Counter

# Creating Counter with String objects

language_counter = Counter(“Classes”)

print(‘language_counter: ‘, language_counter)

# Creating Counter with dict objects

print(‘language_counter: ‘, language_counter)

# Creating Counter with key and value only or we can say that kwargs parameater

print(‘language_counter: ‘, language_counter)

  • In this example first we import the Counter objects from collection.
  • Second create an empty Counter object.
  • Assign different objects to counter object to create a new counter objects.

Let’s see output of program 💻💻:-

You can see in first outuput line it will count the occurence of the iteration of word so in Classes there are 3 times s overe there that’s why s:3 in output.This is basic exmple of Counter now let’s see some useful other example of counter.

Some Useful 💪 examples of Counter :-

In dict object if we pass a key that not in that dict objects so dict objects will return an error and is Keyerror but in counter if we pass a key that not in Counter object so it will not throug the error simpaly it will give the Zero(0) vlaue so we can understand that there is no Key what we searching in that counter object.

Problem 😑:

print(language_counter[“PHP”])

Traceback (most recent call last):

print(language_counter[“PHP”])

Here you can see that we search for PHP key in dict but php key is not dict object so it give a keyError.

Solution 😀:

Here You can see that we got 0 in case of throw the error from counter object.

Let’s see how we can use Counter.elements() 🤔function.

from collections import Counter

language_counter = Counter(

Here In output you can see it will return the iteration of each time over the object. If element count is less than 1 ( element>1) it not consider as a data so it will return nothing.

Let’s see how we can use most_common(n)🤔 in Counter objects.

from collections import Counter

language_counter = Counter(‘PythonClassesP’)

In Ouptput you can see in string there are two words that’s occurrance is two or more that two that’s why in output we got s and p with their number of occurrance.

Return a list of the n most common elements and their counts from the most common to the least.

All airmethic operation 💪💪 in Counter objects :

from collections import Counter

# add two counters together:

print(counter_object1 + counter_object2)

# subtract two counters together:

print(counter_object1 — counter_object2)

# intersection two counters together:

print(counter_object1 & counter_object2)

# union two counters together:

print(counter_object1 | counter_object2)

Originally published at https://www.pythonclasses.online.

--

--

Sonu Gupta
Sonu Gupta

Written by Sonu Gupta

I am a Python developer specialised in Machine Learning Data Science and backend development for complex scalable web apps using Django.

No responses yet