---Advertisement---

Python Counter in Collections with Examples Complete Guide for Beginners 2025 Don’t Miss It

By Bhavani

Updated On:

---Advertisement---
Python Counter in Collections with Examples

Python Counter in Collections: Python Counter is a powerful tool from the collections module that counts the frequency of elements in a container such as lists, tuples, dictionaries, or strings. It’s a subclass of the dictionary class and is useful for tracking the occurrence of elements efficiently.

Python Counter offers several benefits:
✅ Holds data in an unordered collection like a hash table.
✅ Easily counts items in iterable objects like lists, tuples, etc.
✅ Supports arithmetic operations like addition, subtraction, intersection, and union.
✅ Efficiently counts elements from another Counter.


python

from collections import Counter

Counter(iterable)

python

from collections import Counter

list1 = [‘x’, ‘y’, ‘z’, ‘x’, ‘x’, ‘x’, ‘y’, ‘z’]

print(Counter(list1))

Output: Counter({‘x’: 4, ‘y’: 2, ‘z’: 2})


python

from collections import Counter

my_str = “Welcome to Python Counter Guide!”

print(Counter(my_str))

python

from collections import Counter

dict1 = {‘x’: 4, ‘y’: 2, ‘z’: 2}

print(Counter(dict1))

Output: Counter({‘x’: 4, ‘y’: 2, ‘z’: 2})

python

from collections import Counter

tuple1 = (‘x’, ‘y’, ‘z’, ‘x’, ‘x’, ‘x’, ‘y’, ‘z’)

print(Counter(tuple1))

Output: Counter({‘x’: 4, ‘y’: 2, ‘z’: 2})


python

from collections import Counter

print(Counter(“Python Guide”))  # Using string

print(Counter([‘x’, ‘y’, ‘x’])) # Using list

print(Counter({‘x’: 4, ‘y’: 2})) # Using dictionary

print(Counter((‘a’, ‘b’, ‘a’))) # Using tuple

python

from collections import Counter

count = Counter()

count.update(“Python Counter Example”)

print(count)

Output: Counter({‘ ‘: 2, ‘t’: 2, ‘e’: 2, ‘P’: 1, ‘y’: 1, ‘h’: 1, ‘o’: 1, ‘n’: 1, ‘C’: 1, ‘u’: 1, ‘r’: 1, ‘x’: 1, ‘m’: 1, ‘l’: 1})

python

from collections import Counter

count = Counter(“Python Counter Guide”)

print(count[‘o’])  # Output: 2


Python Counter supports arithmetic operations such as:

python

from collections import Counter

counter1 = Counter({‘x’: 4, ‘y’: 2})

counter2 = Counter({‘x’: 1, ‘y’: 5})

print(counter1 + counter2)

Output: Counter({‘y’: 7, ‘x’: 5})

python

print(counter1 – counter2)

Output: Counter({‘x’: 3})

python

print(counter1 & counter2)

Output: Counter({‘x’: 1, ‘y’: 2})

python

print(counter1 | counter2)

Output: Counter({‘x’: 4, ‘y’: 5})



Returns all elements with count > 0.

python

from collections import Counter

counter1 = Counter({‘x’: 5, ‘y’: 2, ‘z’: -2})

print(list(counter1.elements()))

python

from collections import Counter

counter1 = Counter({‘x’: 5, ‘y’: 12})

print(counter1.most_common(1))  # [(‘y’, 12)]


Deducts elements from another Counter.

python

from collections import Counter

counter1 = Counter({‘x’: 5, ‘y’: 12})

counter2 = Counter({‘x’: 2, ‘y’: 5})

counter1.subtract(counter2)

print(counter1)

python

from collections import Counter

counter1 = Counter({‘x’: 5, ‘y’: 12})

counter2 = Counter({‘x’: 2, ‘y’: 5})

counter1.update(counter2)

print(counter1)


✅ Python Counter is a versatile tool for counting data efficiently.
✅ It works with lists, dictionaries, strings, and tuples.
✅ Supports key methods like elements(), most_common(), subtract() and update().
✅ Useful for performing arithmetic operations like addition, subtraction, intersection, and union.

Python Queue

Download Python

Leave a Comment

Index