Counter.most_common

A sorted list of (element, count) pairs. Equal counts are not sorted alphabetically — they stay in first-seen order — and the n least common are one slice away.

Counter methodPython 3.1+Live demo
Common call
Counter(words).most_common(3)
Returns
[('the', 4), ('and', 2), ('cat', 1)] — a list of tuples
Replaces
sorted(d.items(), key=lambda kv: kv[1], reverse=True)[:n]
Watch out
ties are in insertion order, not alphabetical
Counter.most_common(nn — How many pairs to return. None returns every element; 0 or a negative number returns [].type: int | None · default: None=None)
→ list[tuple[element, int]]

Demo

Live evaluation
Rank words by frequency. Leave n empty for None (every word).
Try:
Inputs
textstrsome words
nint | Nonehow many (empty = None)
Code
from collections import Counter
Counter('red blue red green blue red'.split()).most_common(2)
Result
[('red', 3), ('blue', 2)]

For "cabcab" every letter appears twice, so most_common() returns them in first-seen order: c, a, b. The least-common slice walks the ranked list from the end, which also reverses the tie order.

Parameters

NameTypeRequiredDescription
nint | Noneno (None)How many pairs to return. None returns every element; 0 or a negative number returns [].

Return value

list[tuple[element, int]] — (element, count) pairs, highest count first. All of them when n is None.

Common patterns

Top N report
Unpack the pairs directly in the loop.
from collections import Counter
for word, count in Counter(words).most_common(10):
    print(f"{word:<15}{count}")
The single most common element
most_common(1) returns a one-item list — index into it.
from collections import Counter
value, count = Counter(votes).most_common(1)[0]
Deterministic tie-break
Sort yourself when ties must be alphabetical.
from collections import Counter
ranked = sorted(Counter(words).items(), key=lambda kv: (-kv[1], kv[0]))

Examples

1. Top 2 letters
from collections import Counter Counter('abracadabra').most_common(2)
Returns
[('a', 5), ('b', 2)]
2. Ties keep first-seen order
from collections import Counter Counter('abracadabra').most_common(3)
Returns
[('a', 5), ('b', 2), ('r', 2)]
3. n=None returns everything
from collections import Counter Counter('aab').most_common()
Returns
[('a', 2), ('b', 1)]
4. Negative n returns an empty list
from collections import Counter Counter('aab').most_common(-1)
Returns
[]
5. The single winner
from collections import Counter Counter(['yes', 'no', 'yes']).most_common(1)[0][0]
Returns
'yes'
6. Least common
from collections import Counter Counter('aaabbc').most_common()[:-2-1:-1]
Returns
[('c', 1), ('b', 2)]

Pitfalls

1. Expecting a dict back
most_common returns a list of tuples. Wrap it in dict() for key lookups.
result['a']
from collections import Counter
Counter('aab').most_common(1)['a']
TypeError: list indices must be integers or slices, not str
dict(result)
from collections import Counter
dict(Counter('aab').most_common(1))['a']
2
2. Assuming alphabetical ties
Among equal counts the first-seen element wins, so the answer depends on input order.
order-dependent
from collections import Counter
Counter(['b', 'a']).most_common(1)
[('b', 1)]
explicit tie-break
from collections import Counter
min(Counter(['b', 'a']).items(), key=lambda kv: (-kv[1], kv[0]))
('a', 1)

When to use

Use it
  • Ranking by frequency
  • Picking the mode (most frequent value) of a list
Reach for something else
  • The mode of numeric data with a clear error on empty input → statistics.mode / multimode
  • Ties that must break alphabetically → sorted() with a (-count, key) key

Notes

CPython impl
sorted(self.items(), key=itemgetter(1), reverse=True) when n is None, heapq.nlargest(n, …) otherwise — both keep insertion order among ties
Returns
A new list of (element, count) tuples; the Counter is not changed

FAQ

Counter(my_list).most_common(1)[0][0]. most_common(1) returns a one-element list like [('x', 3)], so [0] is the pair and [0][0] the element. It raises IndexError on an empty list.