Skip to the content.

Author: Amin Boulouma, Software Engineer Github source code: https://github.com/aminblm/ai_systems_design_from_scratch Engineering Blog: https://aminblm.github.io/ai_systems_design_from_scratch/blog/

3 Pro-Level Ways to Use collections.Counter for Optimized Aggregation

When building data-intensive applications, you frequently encounter the need to count occurrences—whether it’s tracking log events, analyzing user behavior, or calculating word frequencies. While a defaultdict(int) works, Python provides a dedicated, enterprise-grade tool: collections.Counter.


The Core Concept

A Counter is a specialized dictionary subclass designed specifically for counting hashable objects. It stores elements as dictionary keys and their counts as dictionary values. Beyond simple counting, it provides powerful set-like arithmetic operations that are highly optimized for performance.

Glossary for Beginners


Why We Choose Counter over defaultdict(int)

We choose Counter because it is declarative and feature-rich. defaultdict(int) is a generic tool that you manually increment, whereas Counter is a domain-specific tool that knows how to handle frequencies.

Why X over Y? We choose Counter over defaultdict when we need access to methods like .most_common(), .subtract(), or when we need to perform set arithmetic (e.g., finding the intersection of two counters). It transforms what would be an $O(N)$ loop into a clean, single-method call.


Implementation: The Counter Pattern

Simple Example: Frequency Counting

from collections import Counter

# Tracking frequency of items
items = ["apple", "banana", "apple", "orange", "banana", "apple"]
counts = Counter(items)

print(counts.most_common(1))  # Output: [('apple', 3)]

Complex Example: Multi-Set Arithmetic in Production

In real-world analytics, we often compare two datasets (e.g., inventory tracking or log differences). Counter handles this natively.

from collections import Counter

# Inventory snapshots
stock_initial = Counter(apple=10, banana=5, orange=2)
stock_sold = Counter(apple=3, banana=2)

# Calculate remaining stock using subtraction
remaining = stock_initial - stock_sold

print(dict(remaining))
# Output: {'apple': 7, 'banana': 3, 'orange': 2}

Quick Reference: Counter Operations

Operation Syntax Use Case
Most Common c.most_common(n) Get top N elements
Addition c1 + c2 Merge datasets
Subtraction c1 - c2 Keep only positive counts
Intersection c1 & c2 Find common elements/min counts

Developer Checklist

TL;DR Summary

Stop using manual defaultdict loops for counting. Use collections.Counter to express your intent clearly. It is optimized for performance, provides native set-arithmetic, and includes powerful utility methods like .most_common() that make analytical code both shorter and easier to audit.