collections.Counter.most_common
Find the most common items of a Counter object.
Python 3.13
collections.Counter.most_common([n])Parameters
- nint | Noneoptional
- How many items to return; all of them if not given.
Return value
A list of (item, count) tuples in descending order of count.
Examples
Three most common letters
Python 3.13
from collections import Counter
print(Counter('abracadabra').most_common(3))[('a', 5), ('b', 2), ('r', 2)]All items by descending frequency
Python 3.13
from collections import Counter
print(Counter(['pear', 'apple', 'pear', 'kiwi']).most_common())[('pear', 2), ('apple', 1), ('kiwi', 1)]Details
Items with equal counts are ordered by first occurrence. The rarest item can be taken from the end: counts.most_common()[-1].
