🎓 A1Learner
🔍

Python Counter & defaultdict

Counter is a dict subclass that counts how often each value appears. defaultdict makes a missing value on first use.

Count by hand first

This plain dict counts words with a check for every new word.

counts = {}
for word in ["apple", "banana", "apple"]:
    if word in counts:
        counts[word] += 1
    else:
        counts[word] = 1
print(counts)

Count in one step

Counter does the whole counting loop for you. It keeps the order items were first seen.

from collections import Counter

counts = Counter(["apple", "banana", "apple", "cherry", "banana", "apple"])
print(counts)

Find the most common

most_common sorts by count, highest first. Ties keep the order items were first seen.

print(counts.most_common(2))

Add and subtract counters

Adding merges the counts together. Subtracting drops counts that fall to zero or below.

a = Counter(["apple", "apple", "banana"])
b = Counter(["apple", "cherry"])
print(a + b)
print(a - b)

Group with defaultdict

A defaultdict(list) starts an empty list for a new key. You never test if the key exists.

from collections import defaultdict

groups = defaultdict(list)
for word in ["ant", "bear", "ape", "bee"]:
    groups[word[0]].append(word)
print(dict(groups))

Count with defaultdict

A defaultdict(int) starts a missing count at zero. One += 1 then works for every new key.

counts = defaultdict(int)
for letter in "banana":
    counts[letter] += 1
print(dict(counts))

Example

from collections import Counter

text = "the cat sat on the mat the cat ran"
words = text.split()
counts = Counter(words)
print(counts)
print(counts.most_common(2))
print("the:", counts["the"])

Key points

  • Hashable: a value that can be a dict key, like a string or a number
  • Counter: a dict subclass that counts how often each hashable value appears
  • Use most_common(n) to get the n most frequent items as pairs
  • Use + and - to add or subtract Counter objects count by count
  • defaultdict: a dict that makes a missing value for you on first use
  • Counter subtraction drops any count that reaches zero or below
  • Both tools remove the key check from counting and grouping code.

In this example, text holds one short sentence. The split call turns it into a list of words. Counter counts every word in one step. The print shows the counts in first-seen order. The most_common call lists the top two words. The last line reads the count for the word the. The beginner word counter needed a loop and a key check. This version does the same job in half the lines.

Note

It never keeps a zero or negative count.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does Counter(["a", "a"]) - Counter(["a", "a", "b"]) give?