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

19 lines
342 B
Markdown

---
title: most_frequent
tags: 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.
```py
def most_frequent(lst):
return max(set(lst), key = lst.count)
```
```py
most_frequent([1, 2, 1, 2, 3, 2, 1, 4, 2]) #2
```