python

Find the most repeated item from a list in Python

To find the most repeated or the item that has the highest frequency in the list, you can use the below methods with code examples.

my_list = [3, 5, 9, 3, 5, 3, 4, 2, 1]

result = max(set(my_list), key = my_list.count)

print(result)

# -> 3

In the above code snippet, we are using max() and set() functions of Python to get the element that is most repeated in a list.

We have a list variable my_list that contains multiple items and we are getting the most common element from this list using max() and set() functions and storing the return value in the result variable.

Getting the most common element using mode() function from statistics module

We can also use the mode() function from the statistics Python module to get the most repeated item from a list. We need to pass the list as a parameter to the mode() function and it will return the most frequent item from that list.

Basic Syntax

mode(List)

Code Example

from statistics import mode

numbers = [2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6]

result = mode(numbers)

print(result)

Output

5
Was this helpful?