Menu

gitpiper

most_frequent python Code Snippet in 2024

listbeginner

Last Updated: 9 April 2024

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

python snippet similar to most_frequent For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️