Finding the mode of a list

bluelantern picture bluelantern · May 29, 2012 · Viewed 259.5k times · Source

Given a list of items, recall that the mode of the list is the item that occurs most often.

I would like to know how to create a function that can find the mode of a list but that displays a message if the list does not have a mode (e.g., all the items in the list only appear once). I want to make this function without importing any functions. I'm trying to make my own function from scratch.

Answer

David Dao picture David Dao · Jan 24, 2015

You can use the max function and a key. Have a look at python max function using 'key' and lambda expression.

max(set(lst), key=lst.count)