Files
30-seconds-of-code/snippets/most_frequent.md
Isabelle Viktoria Maciohsek 98b5f67412 Update snippet descriptions
2020-11-02 19:28:27 +02:00

342 B

title, tags
title tags
most_frequent list,beginner

Returns the most frequent element in a list.

  • Use set() to get the unique values in lst.
  • Use max() to find the element that has the most appearances.
def most_frequent(lst):
  return max(set(lst), key = lst.count)
most_frequent([1, 2, 1, 2, 3, 2, 1, 4, 2]) #2