Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save moriarty99779/6e85b7d0543a5f2aa41fc35bbf9c41e1 to your computer and use it in GitHub Desktop.
Save moriarty99779/6e85b7d0543a5f2aa41fc35bbf9c41e1 to your computer and use it in GitHub Desktop.
Python 3 Find Most Frequent Element in List
from collections import Counter
def most_frequent(lst):
data = Counter(lst)
return data.most_common(1) # returns most frequent 1 element
list = [2, 1, 2, 2, 1, 3, 3, 3, 2, 3, 3, 1]
print(most_frequent(list))
# output: [(3, 5)] # 3 is the most frequent element which appears 5 times.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment