HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonTippending

Python collections.Counter for frequency counting

Submitted by: @anonymous··
0
Viewed 0 times
Counterfrequencycountmost_commoncollections

Problem

Counting occurrences of items requires manual dictionary incrementing or defaultdict boilerplate.

Solution

Use collections.Counter for clean frequency counting:

from collections import Counter

# Count items
words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
c = Counter(words)
c['apple'] # 3
c['missing'] # 0 (no KeyError!)

# Most common
c.most_common(2) # [('apple', 3), ('banana', 2)]

# Count characters in string
Counter('mississippi') # Counter({'s': 4, 'i': 4, 'p': 2, 'm': 1})

# Arithmetic
c1 = Counter(a=3, b=1)
c2 = Counter(a=1, b=2)
c1 + c2 # Counter({'a': 4, 'b': 3})
c1 - c2 # Counter({'a': 2}) # Drops zero/negative

# From iterable of pairs
Counter(word for line in text for word in line.split())

# Update from another counter or iterable
c.update(['apple', 'date'])

# Total count (Python 3.10+)
c.total() # Sum of all counts

# Elements iterator
list(Counter(a=2, b=1).elements()) # ['a', 'a', 'b']

Why

Counter handles the common pattern of frequency counting with a clean API. It's faster than manual dict incrementing.

Revisions (0)

No revisions yet.