Counters in Python | Set 2 (Accessing Counters)
Last Updated :
06 Sep, 2023
Counters in Python | Set 1 (Initialization and Updation)
Counters in Python | Set 2
Once initialized, counters are accessed just like dictionaries. Also, it does not raise the KeyValue error (if key is not present) instead the value's count is shown as 0.
Example: In this example, we are using Counter to print the key and frequency of that key. The elements present inside the frequency map are printed along with their frequency and if the element is not present inside the Counter map then the element will be printed along with 0.
Python3
from collections import Counter
# Create a list
z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
col_count = Counter(z)
print(col_count)
col = ['blue','red','yellow','green']
# Here green is not in col_count
# so count of green will be zero
for color in col:
print (color, col_count[color])
Output: <
Counter({'blue': 3, 'red': 2, 'yellow': 1})
blue 3
red 2
yellow 1
green 0
elements() method of Counter in Python
The elements() method returns an iterator that produces all of the items known to the Counter. Note: Elements with count <= 0 are not included.
Example : In this example, the elements inside the Counter would be printed by using the elements() method of Counter.
Python3
# Python example to demonstrate elements()
from collections import Counter
coun = Counter(a=1, b=2, c=3)
print(coun)
print(list(coun.elements()))
Output :
Counter({'c': 3, 'b': 2, 'a': 1})
['a', 'b', 'b', 'c', 'c', 'c']
most_common() method of Counter in Python
most_common() is used to produce a sequence of the n most frequently encountered input values and their respective counts. If the parameter 'n' is not specified or None is passed as the parameter most_common() returns a list of all elements and their counts.
Example: In this example, the element with the most frequency is printed followed by the next-most frequent element by using most_common() method inside Counter in Python.
Python3
from collections import Counter
coun = Counter(a=1, b=2, c=3, d=120, e=1, f=219)
# This prints 3 most frequent characters
for letter, count in coun.most_common(3):
print('%s: %d' % (letter, count))
Output :
f: 219
d: 120
c: 3
Similar Reads
set() Constructor in Python In Python, the set() constructor is used to create a set object. A set is a built-in data type that stores an unordered collection of unique elements. The set() constructor can be used to create an empty set or convert other data types (like lists, tuples, or strings) into a set.Example:Python# Exam
2 min read
Python - Counter.items(), Counter.keys() and Counter.values() Counter class is a special type of object data-set provided with the collections module in Python3. Collections module provides the user with specialized container datatypes, thus, providing an alternative to Pythonâs general-purpose built-ins like dictionaries, lists and tuples. Counter is a sub-cl
3 min read
Iterate over a set in Python The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. There are several ways to access and process each element of a set in Python, but the sequence may change with each execution. Let's explore different ways to iterate over a s
2 min read
Python Collections Counter Counters are a subclass of the dict class in Python collections module. They are used to count the occurrences of elements in an iterable or to count the frequency of items in a mapping. Counters provide a clean and efficient way to tally up elements and perform various operations related to countin
4 min read
Access List Items in Python Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read
Count set bits using Python List comprehension set bits means finding how many 1s are in the binary form of a number. The set bit is any bit that is 1. List comprehension offers a quick and simple way to count these set bits. In this article, we will count set bits using Python list comprehension. Using bin()bin() function converts a number to b
2 min read