
👉Python Counter in Collections with Example
👉What is Python Counter?
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.
👉Why Use Python Counter?
👉Python Counter in Collections
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 Counter in Collections Syntax
python
from collections import Counter
Counter(iterable)
👉Python Counter in Collections Example Usage with List
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 Counter with Different Data Types
✅ String Example
python
from collections import Counter
my_str = “Welcome to Python Counter Guide!”
print(Counter(my_str))
Output:
Counter({‘ ‘: 5, ‘e’: 4, ‘o’: 3, ‘t’: 3, ‘n’: 2, ‘W’: 1, ‘l’: 1, ‘c’: 1, ‘m’: 1, ‘P’: 1, ‘y’: 1, ‘h’: 1, ‘G’: 1, ‘d’: 1, ‘!’: 1})
✅ Dictionary Example
python
from collections import Counter
dict1 = {‘x’: 4, ‘y’: 2, ‘z’: 2}
print(Counter(dict1))
Output: Counter({‘x’: 4, ‘y’: 2, ‘z’: 2})
✅ Tuple Example
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})
👉Accessing, Initializing, and Updating Counters
✅ Initialize Counter with Data Types
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
✅ Updating Counter Values
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})
✅ Accessing Counter Elements
python
from collections import Counter
count = Counter(“Python Counter Guide”)
print(count[‘o’]) # Output: 2
👉Arithmetic Operations on Counter
Python Counter supports arithmetic operations such as:
✅ Addition
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})
✅ Subtraction
python
print(counter1 – counter2)
Output: Counter({‘x’: 3})
✅ Intersection (Minimum values)
python
print(counter1 & counter2)
Output: Counter({‘x’: 1, ‘y’: 2})
✅ Union (Maximum values)
python
print(counter1 | counter2)
Output: Counter({‘x’: 4, ‘y’: 5})
👉Important Python Counter Methods
✅ elements() Method
Returns all elements with count > 0.
python
from collections import Counter
counter1 = Counter({‘x’: 5, ‘y’: 2, ‘z’: -2})
print(list(counter1.elements()))
✅ most_common() Method
Returns the most common elements.
python
from collections import Counter
counter1 = Counter({‘x’: 5, ‘y’: 12})
print(counter1.most_common(1)) # [(‘y’, 12)]
✅ subtract() Method
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)
✅ update() Method
Updates Counter with new elements.
python
from collections import Counter
counter1 = Counter({‘x’: 5, ‘y’: 12})
counter2 = Counter({‘x’: 2, ‘y’: 5})
counter1.update(counter2)
print(counter1)
👉Python Counter in Collections Summary
✅ 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.