Python most common element in a list

Posted by Richard on Stack Overflow See other posts from Stack Overflow or by Richard
Published on 2009-10-05T06:35:44Z Indexed on 2010/04/14 0:43 UTC
Read the original article Hit count: 523

Filed under:
|

What is an efficient way to find the most common element in a Python list?

My list items may not be hashable so can't use a dictionary. Also in case of draws the item with the lowest index should be returned. Example:

>>> most_common(['duck', 'duck', 'goose'])
'duck'
>>> most_common(['goose', 'duck', 'duck', 'goose'])
'goose'

© Stack Overflow or respective owner

Related posts about python

Related posts about list