359 B
359 B
title, tags
| title | tags |
|---|---|
| most_frequent | list,beginner |
Returns the most frequent element in a list.
- Use
set(list)to get the unique values in thelistcombined withmax()to find the element that has the most appearances.
def most_frequent(list):
return max(set(list), key=list.count)
most_frequent([1, 2, 1, 2, 3, 2, 1, 4, 2]) #2