Explain Codes LogoExplain Codes Logo

Find the most common element in a list

python
prompt-engineering
functions
collections
Nikita BarsukovbyNikita Barsukov·Nov 16, 2024
TLDR

To quickly find the most common element in a Python list, apply Counter from the collections module. Pass your list to Counter and obtain the top element with most_common(1). This function returns a list of tuples — the first tuple carries the element and its count. Utilize indexing via [0][0] to extract the element:

from collections import Counter my_list = [1, 1, 2, 1, 3] most_common_element = Counter(my_list).most_common(1)[0][0] print(most_common_element) # Prints: 1

Execute this, and you will see 1 — the most frequent item in the list.

Non-hashable items: What now?

Alas, Counter is helpless when facing non-hashable entries, such as dictionaries or internal lists. Don't fret! Python has you covered with groupby from itertools:

from itertools import groupby my_list = [['apple'], ['apple'], ['banana'], ['apple'], ['cherry']] # Remember: groupby needs a well-behaved (sorted) list my_list.sort() # Group based on identity, then select the King of Frequency most_common_element = max(groupby(my_list), key=lambda x: len(list(x[1])))[0] print(most_common_element) # Prints: ['apple']

Python's helper: mode()

Python 3.8 dropped a fabulous function mode in the statistics module, allowing you to pinpoint the most common element site-wide, including the elusive non-hashable types. Keep in mind, though, that when faced with a tie, mode sides with the first entry:

from statistics import mode my_list = [1, 2, 2, 3, 3, 3, 2] most_common_element = mode(my_list) # Python's way of saying "eeny, meeny, miny, moe" print(most_common_element) # Prints: 2

Considering item order

When victors come in pairs (ties), how do you pick one? If your trusty Python version is 3.6+, Counter serves you the first-seen contestant on a silver platter. However, should you resort to max(set(lst), key=lst.count), the jalopy of index order reigns supreme.

Don't forget about time!

Time complexity is key when choosing a method. Counter and mode, with their O(n) time frets, outdo the O(n^2) time load of max(set(lst), key=lst.count) on long lists.

Swing at edge cases

While Counter and mode are your everyday heroes, edge cases can stump them. For these tricky cases, either spitball map-reduce patterns or numpy arrays for numerical data.

Counter? More like "Can't-er"

Counter backs down from unhashable items, but luckily, groupby from itertools is a worthy substitute, provided your rebels can sort their differences.

Nested lists and dictionaries

For lists of lists or dictionaries, roll your sleeves and conjure a function to compare items based on your rules.

Lambdas: small, but mighty

Despite their short stature, lambda functions pack a punch in the max function, allowing for custom comparisons:

my_list = ['apple', 'banana', 'cherry', 'apple', 'cherry', 'cherry'] most_common_element = max(set(my_list), key=lambda x: (my_list.count(x), -my_list.index(x))) print(most_common_element) # I found the cherry on top!