Files
30-seconds-of-code/snippets/most_frequent.md
Isabelle Viktoria Maciohsek 19f636408c Make expertise a field
2022-03-01 20:27:27 +02:00

437 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Most frequent element list beginner 2019-10-12T00:40:49+03:00 2020-11-02T19:28:27+02:00

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