Find the most common element in a list
Borrowing from here, this can be used with Python 2.7:
from collections import Counter
def Most_Common(lst):
data = Counter(lst)
return data.most_common(1)[0][0]
Works around 4-6 times faster than Alex's solutions, and is 50 times faster than the one-liner proposed by newacct.
To retrieve the element that occurs first in the list in case of ties:
def most_common(lst):
data = Counter(lst)
return max(lst, key=data.get)
A simpler one-liner:
def most_common(lst):
return max(set(lst), key=lst.count)