DEV Community

loading...
Cover image for Python's Collections Module: Counter

Python's Collections Module: Counter

Kathan Vakharia
DS-Algo Tenderfoot | Aspiring Gopher | DataScience Enthusiast | Python & C++ 💕
Updated on ・2 min read

Introduction

A Counter is a dict subclass for counting hashable objects → any object which is not mutable.

It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values.

#To use it, ofcourse we need to first import it
from collections import Counter
Enter fullscreen mode Exit fullscreen mode

Using Counter

myList = ['k', 'k', 1, 1, 2, 2, 1, 2, 3, 3, 1, 'a']
myString = "CodeBlooded"

# simply pass any iterable
# Counter(iterable) returns a Counter obj
count1 = Counter(myList)
count2 = Counter(myString)

#How does a Counter obj look?
print(count1, count2, sep='\n')

"""Output
Counter({1: 4, 2: 3, 'k': 2, 3: 2, 'a': 1})
Counter({'o': 3, 'd': 3, 'e': 2, 'C': 1, 'B': 1, 'l': 1})
""""
Enter fullscreen mode Exit fullscreen mode

💡 As counter is a subclass of dictionary, it has all the methods of the dictionary.

most_common(n) method

The most_common(n) returns a list of n most common objects along with their respective counts.

from collections import Counter

myList = ['k', 'k', 1, 1, 2, 2, 1, 2, 3, 3, 1, 'a']
myString = "CodeBlooded"

# simply pass any iterable
# Counter(iterable) returns a Counter object
count1 = Counter(myList)
count2 = Counter(myString)


print(count1, count2, sep='\n')

# If n is omitted or None, most_common()
# returns all elements in the counter.
print(count1.most_common(2))
print(count2.most_common(3))

"""Output
Counter({1: 4, 2: 3, 'k': 2, 3: 2, 'a': 1})
Counter({'o': 3, 'd': 3, 'e': 2, 'C': 1, 'B': 1, 'l': 1})
[(1, 4), (2, 3)]
[('o', 3), ('d', 3), ('e', 2)]
"""
Enter fullscreen mode Exit fullscreen mode

Common Patterns with Counter

For a given Counter object c,
image

💡 Feel free to fire up your ipython or python shell and try this commands out for better understanding :)

Let's wrap up this post with a problem where Counter is very helpful.

Finding most frequent word using Counter in a given string

from collections import Counter

string = "string with repeated word Jello So what is Jello Who cares what is Jello anyways but Jello must be repeated Jello number of Jello times"

# You can use re.split() for more complex patterns!
words = string.split()
words_count = Counter(words)
print("Most Occurring word is",
      words_count.most_common(1))

"""Output
Most Occurring word is [('Jello', 6)]
"""
Enter fullscreen mode Exit fullscreen mode

❓ if you notice, while printing the Counter object - it doesn't maintain the order of elements present in the iterable passed. Do you know why?

Try googling or duckduckgoing! I will answer this question in next post 😉

Discussion (0)