Open In App

Python Collections Counter

Last Updated : 03 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Counter is a subclass of Python’s dict from the collections module. It is mainly used to count the frequency of elements in an iterable (like lists, strings, or tuples) or from a mapping (dictionary). It provides a clean and efficient way to tally items without writing extra loops and comes with helpful built-in methods.

Example:

Python
from collections import Counter
# Create a list of items
num = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

# Use Counter to count occurrences
cnt = Counter(num)
print(cnt)

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

Counter object shows that 4 appears 4 times, 3 appears 3 times, 2 appears 2 times and 1 appears once.

Syntax

collections.Counter([iterable-or-mapping])

Parameters (all optional):

  • iterable: Sequence like list, tuple, or string.
  • mapping: Dictionary with elements as keys and counts as values.
  • keyword arguments: String keys mapped to counts.

Return Type: Returns a collections.Counter object (dictionary-like).

Why use Counter() instead of a normal dictionary?

  • Quickly counts elements in a list, string or any iterable without writing extra loops.
  • Great for data summaries like counting words, votes or item frequencies.
  • Includes helpful built-in methods like most_common() and elements() for easier processing.
  • Cleaner and more efficient than using regular dictionaries for counting.
  • Supports flexible input types, works with lists, dictionaries or even keyword arguments.

Creating a Counter

We can create Counters from different data sources.

Python
from collections import Counter
ctr1 = Counter([1, 2, 2, 3, 3, 3]) # From a list
ctr2 = Counter({1: 2, 2: 3, 3: 1}) # From a dictionary
ctr3 = Counter('hello') # From a string

print(ctr1)
print(ctr2)
print(ctr3)

Output
Counter({3: 3, 2: 2, 1: 1})
Counter({2: 3, 1: 2, 3: 1})
Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})

Explanation:

  • ctr1 counts numbers from a list.
  • ctr2 directly takes a dictionary of counts.
  • ctr3 counts characters in the string "hello".

Refer to collection module to learn in detail.

Accessing Counter Elements

We can access the count of each element using the element as the key. If an element is not in the Counter, it returns 0.

Example:

Python
from collections import Counter
ctr = Counter([1, 2, 2, 3, 3, 3])

# Accessing count of an element
print(ctr[1])  
print(ctr[2])  
print(ctr[3])  
print(ctr[4])  # (element not present)

Output
1
2
3
0

Explanation: Counter returns the count of each element. If an element does not exist (4 in this case), it returns 0.

Updating counters

Counters can be updated by adding new elements or by updating the counts of existing elements. We can use the update() method to achieve this.

Example:

Python
from collections import Counter
ctr = Counter([1, 2, 2])

# Adding new elements
ctr.update([2, 3, 3, 3])
print(ctr)  

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

Explanation:

  • Initially, the Counter has {1:1, 2:2}.
  • After update, 2 increases to 3 and 3 appears 3 times.

Counter Methods

1. elements()

Returns an iterator over elements repeating each as many times as its count. Elements are returned in arbitrary order.

Python
from collections import Counter
ctr = Counter([1, 2, 2, 3, 3, 3])
items = list(ctr.elements())
print(items)  

Output
[1, 2, 2, 3, 3, 3]

Explanation: elements() method expands the Counter back into its original-like sequence. Each element is repeated as many times as its count.

2. most_common()

Returns a list of the n most common elements and their counts from the most common to the least. If n is not specified, it returns all elements in the Counter.

Python
from collections import Counter
ctr = Counter([1, 2, 2, 3, 3, 3])
common = ctr.most_common(2)
print(common) 

Output
[(3, 3), (2, 2)]

Explanation:

  • The most common number is 3, appearing 3 times.
  • The second most common is 2, appearing 2 times.
  • Only the top 2 elements are shown since we passed 2 as an argument.

3. subtract()

Subtracts element counts from another iterable or mapping. Counts can go negative.

Python
from collections import Counter
ctr = Counter([1, 2, 2, 3, 3, 3])
ctr.subtract([2, 3, 3])
print(ctr)  

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

Explanation:

  • 2 count reduced from 2 -> 1.
  • 3 count reduced from 3 -> 1.
  • 1 remains unchanged.

Note: Counts can even go negative if subtraction exceeds the original count.

Arithmetic Operations on Counters

Counters support addition, subtraction, intersection union operations, allowing for various arithmetic operations.

Example:

Python
from collections import Counter
ctr1 = Counter([1, 2, 2, 3])
ctr2 = Counter([2, 3, 3, 4])

print(ctr1 + ctr2)   # Addition
print(ctr1 - ctr2)   # Subtraction 
print(ctr1 & ctr2)   # Intersection
print(ctr1 | ctr2)   # Union

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

Counters in Python - Part 1

Explore