Skip to content

Instantly share code, notes, and snippets.

@ettorerizza
Created March 12, 2020 08:10
Show Gist options
  • Save ettorerizza/0275abdcc3b89c3a8f483e6d92ff62e8 to your computer and use it in GitHub Desktop.
Save ettorerizza/0275abdcc3b89c3a8f483e6d92ff62e8 to your computer and use it in GitHub Desktop.
Most common elements in a list (with ties)
def most_commons(List):
"""Return a new list with the most common elements
in a list
"""
from collections import Counter
count = Counter(List)
freq_list = count.values()
max_cnt = max(freq_list)
total = freq_list.count(max_cnt)
most_commons = count.most_common(total)
return [elem[0] for elem in most_commons]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment