Find the most common element in a list
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:
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
:
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:
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:
Was this article helpful?