most_common() is one of the most useful methods on Python’s Counter class from the collections module. It quickly gives you the n most frequent items in a collection, sorted by count descending — perfect for finding top words, most common categories, frequent errors, or popular items.
While you could sort a dictionary by value manually, most_common() does it efficiently and returns a clean list of (item, count) tuples — exactly what you usually need.
Basic Syntax
counter.most_common([n]) # n is optional
- If n is given ? returns the n most common items
- If omitted ? returns all items sorted by count (highest first)
Simple Example
from collections import Counter
fruits = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
counts = Counter(fruits)
print(counts.most_common(2))
# [('apple', 3), ('banana', 2)]
print(counts.most_common())
# [('apple', 3), ('banana', 2), ('cherry', 1)]
Real-World Use Cases
1. Top words in text
text = "the quick brown fox jumps over the lazy dog the dog sleeps"
words = text.split()
word_counts = Counter(words)
top_words = word_counts.most_common(3)
print(top_words)
# [('the', 2), ('dog', 2), ('quick', 1)]
2. Most common errors in logs
log_lines = ["ERROR: connection failed", "INFO: login success", "ERROR: timeout", "ERROR: connection failed"]
errors = [line.split('ERROR: ')[1] for line in log_lines if line.startswith('ERROR')]
error_counts = Counter(errors)
print(error_counts.most_common())
# [('connection failed', 2), ('timeout', 1)]
3. Top categories in dataset
categories = ['sales', 'marketing', 'sales', 'support', 'sales', 'marketing']
top_categories = Counter(categories).most_common(2)
print(top_categories) # [('sales', 3), ('marketing', 2)]
Advanced Tips (2026)
- Combine with
heapq.nlargest()for very large counters (memory efficient) - Use
Counter.most_common()with negative counts to get least common - For huge data: switch to
polarspl.col('column').value_counts().sort(descending=True)— often 5–20× faster than Counter on big datasets - Combine with
collections.defaultdict(int)when you don’t need full Counter features
Conclusion
most_common() is one of those small tools that saves time every day. Whether you're analyzing logs, text, survey results, sales data, or any frequency count, it turns messy iteration into one clean call. In 2026, with massive datasets and real-time analytics, reach for Counter.most_common() first — then consider Polars for scale.
Master this method, and your code becomes more readable, faster to write, and easier to maintain.