Explain Codes LogoExplain Codes Logo

How to count the frequency of the elements in an unordered list?

python
collections
counter
groupby
Nikita BarsukovbyNikita Barsukov·Aug 12, 2024
⚡TLDR

To count element frequencies in a list, Python's collections.Counter comes to the rescue:

from collections import Counter frequency = Counter(['apple', 'banana', 'apple', 'pear', 'banana', 'orange']) print(frequency) # Output: Counter({'apple': 2, 'banana': 2, 'pear': 1, 'orange': 1})

Presto! You get a dictionary-like tally where each item gets counted.

Insights and advanced usage of Counter

Modify counts with math operations

Counter objects play nice with arithmetic operations like + and -:

c1 = Counter(['apple', 'banana', 'apple']) c2 = Counter(['banana', 'orange']) c3 = c1 + c2 # As easy as baking a pie! print(c3) # Output: Counter({'apple': 2, 'banana': 2, 'orange': 1})

Silicon Valley's top (n) fruits

To find the n most common items, most_common(n) is the cool kid in town:

print(frequency.most_common(2)) # Output: [('apple', 2), ('banana', 2)] # Fun fact: Apples and bananas are popular in Silicon Valley!

Picking out unique items and counts

.keys() bags you unique elements, while .values() delivers their frequencies:

unique_elements = frequency.keys() print(list(unique_elements)) # Output: ['apple', 'banana', 'pear', 'orange'] frequencies_list = frequency.values() print(list(frequencies_list)) # Output: [2, 2, 1, 1]

Old school frequency count

When Counter isn't an option, defaultdict or dict comprehension come in handy:

from collections import defaultdict def count_frequency(items): freqs = defaultdict(int) for item in items: freqs[item] += 1 return freqs print(count_frequency(['apple', 'banana', 'apple'])) # Output: defaultdict(<class 'int'>, {'apple': 2, 'banana': 1}) # No Counter? No problem!

Dict comprehension offers a „sleek one-liner“ in Python 2.7+:

items = ['apple', 'banana', 'apple'] frequency = {item: items.count(item) for item in set(items)} print(frequency) # Output: {'apple': 2, 'banana': 1}

Spotting the underdogs

To highlight the least common elements, sort elements() in ascending order:

sorted_elements = sorted(frequency.elements()) print(sorted_elements) # Output: ['orange', 'pear', 'apple', 'apple', 'banana', 'banana']

The underdogs ('orange' and 'pear') are up front!

When to call upon groupby

When dealing with sorted lists, prefer itertools.groupby:

from itertools import groupby sorted_list = sorted(['apple', 'banana', 'apple', 'banana', 'orange']) grouped = {k: len(list(g)) for k, g in groupby(sorted_list)} print(grouped) # Output: {'apple': 2, 'banana': 2, 'orange': 1} # Sorted, grouped, and counted—like magic!

Frequency vs elements

Remember: Each frequency in the output list corresponds to a unique value in the input list.