cft

An Introduction to Python Counter

Python Counter is an interesting and useful data structure. Let’s dive deeper and see how it functio


user

Chaitanya Baweja

3 years ago | 4 min read

There are several well known built-in collections in Python like list, dict, set, tuple, etc. These act as containers that are used to store collections of data.

Several other modules have been developed to incorporate additional data structures. Collections is one such module.

Best Coding Languages to Learn in 2019 | Data Driven Investor

During my years as an undergrad, I skipped many night-outs to pick up Java hoping it would one day help me get ahead in…

www.datadriveninvestor.com

The collections module has a variety of data structures like Counter, OrderedDict, ChainMap, etc. In this story, we will present the Counter class. We will initially discuss some basic data structure operations and then look at some interesting additional functions.

Introduction

Python Counter is a subclass of the dict or dictionary class. It keeps track of the frequency of each element in the container.

It takes as argument an iterable object (like list) and returns back a dictionary. In this dictionary,

  1. Key: an element in the iterable
  2. Value: frequency of the element in the iterable

Counter counts hashable objects in Python.

Here’s a simple example:

Creating Counter Objects

Counter objects can be defined in multiple ways:

  1. Using a List, String or Tuple

Here, we pass a list to the Counter() function. In return, we receive a dictionary that tells us that the given argument has 3 ‘a’, 2 ‘b’, and 1 ‘c’.

my_count = Counter(['a','b','c','a','b','a'])
my_count# Output
# Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})

Similarly, we can use a Tuple.

my_count = Counter(('a','b','c','a','b','a'))
my_count# Output
# Counter({‘a’: 3, ‘b’: 2, ‘c’: 1})

Or a Python string.

my_count = Counter("Hello World")
my_count# Output
# Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, ' ': 1, 'W': 1, 'r': 1, 'd': 1})

Observe that counts are always displayed in descending order.

It is important to note that a Python set only holds unique items, i.e., each element can only occur once in a set. So, it does not make any sense to use the Counter()function with a Python set.

2. Using a Dictionary

A dictionary can also be provided as an argument to the Counter() function. The value of a key should be the ‘count’ of that key.

my_count = Counter({'a':3, 'b':2, 'c':1})
my_count# Output
# Counter({'a':3, 'b':2, 'c':1})

3. Using Keyword Arguments

We can feed the counts manually using keyword arguments.

my_count = Counter(i = 1, c = 2, e = 3)
my_count# Output
# Counter({'e': 3, 'c': 2, 'i': 1})

Notice how in the output, the counts were arranged in descending order.

Updating a Python Counter

We can also declare an empty Python Counter. Later on, we add values to it using the update() method.

my_count = Counter()   # defining an empty Countermy_count.update("a")   # adding values to my_count
my_count# Output
# Counter({a:1})

The update() method basically adds new values to an existing counter. We can update this counter further and keep adding more values. For example:

my_count.update("ab")
my_count# Output
# Counter({'a': 2, 'b': 1})

Update operation adds another ‘a’ and a new element ‘b’ to the counter.

Update method adds new values to an existing counter.

Accessing Counts

We can access the count of a particular element by using it as an index to our Python Counter. This is similar to its super-class, a Python Dictionary.

In a dictionary, when we send as argument a key that is not present, we receive a KeyError. But in a Counter, we get zero.

print(my_count['b'])
# 1print(my_count['c'])
# 0

When you try accessing an element that is not present, Counter object will return zero.

Reassigning Counts in Python

Similar to Dictionaries, Counters are also mutable. We can reassign a count using:

# To reassign 'b' to 2
my_count['b'] = 2
my_count# Output
# Counter({'a': 2, 'b': 2})# To clear a Python counter
my_count.clear()
my_count# Output
# Counter()

Till now, we have seen some basic data structural operations like adding, accessing and removing elements from a Python Counter.

As, Counter is a subclass of dict, it has all the methods of dict class. But, Counter has three additional functions: Elements, Most_common and Subtract.

The elements() Method

We use the elements() function to list all the elements in a Counter object. It returns an iterator with the values in Counter object.

Look at the following example:

my_count = Counter({'a':1,'b':2, 'd':5, 'c':4})
my_count.elements()
# <itertools.chain object at 0x7f21ccbd8898>print(list(my_count.elements()))

Output:

['a', 'b', 'b', 'd', 'd', 'd', 'd', 'd', 'c', 'c', 'c', 'c']

We first created a Counter object using a dictionary as the argument. The elements() function returns an iterator which is passed as an argument to the list() function.

elements() function lists all the elements in a Counter object

Notice how the output is no longer in descending order but is in an order similar to the input argument.

Accessing the Most Common Values

We can retrieve the highest frequency values using the most_common() function of the Counter object. For example,

my_count.most_common()
# Output
[('d', 5), ('c', 4), ('b', 2), ('a', 1)]

We receive a list which is sorted in the decreasing order of frequency. The list is formed of tuples.

We can also pass an argument to this function, to get the n most common values in the object. Take a look:

my_count.most_common(1)
# Output
[('d',5)]my_count.most_common(2)
# Output
[('d',5), ('c',4)]

We can also use the most_common() function to find the n least common elements.

my_count.most_common()[:-n:-1]

The most_common() function returns a list of tuples containing an element and its corresponding frequency sorted in the decreasing order.

The subtract Function

The subtract()is the opposite of the update() function. It takes an iterable (list,string,tuple) or a dictionary as an argument and deducts elements count using that argument. For example:

my_count = Counter(['hi','hi','hello'])
print("Original Counter : ", my_count)to_deduct = ['hi']
my_count.subtract(to_deduct)
print("New Counter : ", my_count)

Output:

Original Counter :  Counter({'hi': 2, 'hello': 1})
New Counter : Counter({'hi': 1, 'hello': 1})

The subtract() function deducts 1 count from the key 'hi'.

Conclusion

Python Counter is a container that keeps track of the number of occurrences of a value. Here are some key takeaways:

  1. Python Counter is a subclass of the dict or dictionary class.
  2. Counts are always displayed in descending order.
  3. Update method adds new values to an existing counter.
  4. When you try accessing an element that is not present, Counter object will return zero.
  5. elements() function lists all the elements in a Counter object.
  6. The most_common() function returns a list of tuples containing an element and its corresponding frequency sorted in the decreasing order.
This article was originally published by Chaitanya Baweja on medium.

Upvote


user
Created by

Chaitanya Baweja

Machine Learning Engineer | Python | Data Analytics | Economics | Physics https://medium.com/@chaitanyabaweja1


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles