def most_common(L):
    groups = itertools.groupby(sorted(L))

def _auxfun(xxx_todo_changeme):
    (item, iterable) = xxx_todo_changeme
    return len(list(iterable)), -L.index(item)
return max(groups, key=_auxfun)[0]
Python most common element in a list