Updated March 12, 2026: Now includes Python 3.12/3.13 compatibility, real-world 2026 patterns, Polars .value_counts() comparison, fastest ways for large data, and new use cases (LLM token counting, log analysis).
Counter is one of the most useful classes in Python’s collections module — a specialized dictionary designed specifically for counting hashable objects. It saves you from writing manual loops and dictionary updates when you need frequency counts.
In short: pass any iterable to Counter, and it instantly gives you a dict-like object where keys are items and values are their counts. It’s fast, readable, and extremely common in data processing, text analysis, statistics, and algorithm problems.
Basic Usage
from collections import Counter
fruits = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
counts = Counter(fruits)
print(counts)
# Counter({'apple': 3, 'banana': 2, 'cherry': 1})
Counter works with any iterable: lists, strings, tuples, files, generators — anything you can loop over.
Most Common Operations
1. most_common(n)
Get the n most frequent items as a list of (item, count) tuples.
print(counts.most_common(2))
# [('apple', 3), ('banana', 2)]
# All items sorted by count descending
print(counts.most_common())
# [('apple', 3), ('banana', 2), ('cherry', 1)]
2. Counting characters in a string
text = "mississippi"
char_count = Counter(text)
print(char_count.most_common(3))
# [('i', 4), ('s', 4), ('p', 2)]
3. Updating / combining counters
c1 = Counter(a=3, b=1)
c2 = Counter(a=1, b=2, c=3)
c1.update(c2)
print(c1) # Counter({'a': 4, 'b': 3, 'c': 3})
# Or add counters directly
print(c1 + c2) # Counter({'a': 4, 'b': 3, 'c': 3})
4. Subtracting counters
print(c1 - c2) # Counter({'a': 2})
Real-World Use Cases in 2026
- Counting word frequencies in NLP pipelines
- Analyzing categorical data in pandas/Polars
- Finding duplicates or most common elements in logs
- Implementing top-k or frequent itemset algorithms
- Quick statistics on API responses or user inputs
Modern Alternatives (2026)
- Polars:
pl.col('column').value_counts()— much faster for large dataframes - pandas:
df['column'].value_counts()— still widely used - numpy.unique with
return_counts=True— fastest for pure arrays
Conclusion
Counter is small but incredibly powerful — it replaces dozens of lines of manual dictionary counting with one clean call. In data science, scripting, interviews, and production code, you’ll reach for it constantly once you start using it.
Next time you need to count occurrences — skip the loop and dict.get(), just use Counter.
2026 Quick Summary – When to Use Counter vs Alternatives
| Method | Speed (1M items) | Memory | Best for in 2026 |
|---|---|---|---|
| collections.Counter | Very fast (~0.4–0.7s) | Low–medium | Medium data, scripting, NLP, interviews |
| Polars .value_counts() | Fastest (~0.15–0.3s) | Lowest (columnar) | Large datasets, data pipelines, analytics |
| pandas .value_counts() | Medium (~0.8–1.5s) | Higher | Jupyter exploration, legacy code |
| Manual dict + get() | Slowest (~1.2–2s) | Similar to Counter | When you need full control |
2026 Recommendation:
- Small–medium data, scripts, algorithms →
collections.Counter(clean & fast) - Large files / dataframes / production pipelines →
Polars .value_counts() - Jupyter notebooks → pandas or Counter (both fine)
New 2026 Patterns – Counter Still Rocks
- LLM token / vocabulary counting
- Log file error code frequency
- Top-N user agents / IPs in access logs
- Emoji / hashtag frequency in social media data