Change language

Python | The item with the highest frequency in the list

Method # 1: The Naive Method
As a brute force method, we simply count all the elements and then just return the number of elements that remains at the maximum at the end. This is the main technique one might think of doing when faced with this problem.

# Python3 demo code
# to get the most frequent element
# using the naive method

 
# initializing list

test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , < / code> 5 , 4 ]

 
# print original list

print ( "Original list:" + str (test_list))

  
# using a naive method for
# get the most frequent element

max = 0

res = test_list [ 0 ]

for i in test_list:

freq = test_list.count (i)

if freq"  max :

max = freq

res = i

 
# print result

print ( " Most frequent number is: " + str (res))

Output:

 Original list: [9, 4, 5, 4, 4, 5, 9, 5 , 4] Most frequent number is: 4 

Method # 2: Using max () + set ()
Conversion list to set and maximize function in relation to The number of each number in the list makes this task easy and is the most elegant way to accomplish this.

# Python3 demo code
# to get the most frequent element
# using max () + set ()

 
# initializing list

test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]

 
# print original list

print ( "Original list:" + str (test_list))

 
# using max () + set () to
# get the most frequent element

res = max ( set (test_list), key = test_list.count)

  
# print result

print ( "Most frequent number is:" + str (res))

Output:

 Original list: [9, 4, 5, 4, 4, 5, 9, 5, 4] Most frequent number is: 4 

Method # 3: Using statistics.mode()
Mode is denoted as an element of maximum frequency in mathematics, and python allocates integer library for the aggregate function, and this can also be used to solve this problem.

# Python3 demo code
# to get the most frequent item
# using statistics.mode ()

import statistics

 
# initializing list

test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]

 
# print original list

pr int ( "Original list:" + str (test_list))

 
# using statistics.mode () for
# get the most frequent element

res = statistics.mode (test_list)

 
# print result

print ( "Most frequent number is:" + str (res))

Output:

 Original list: [9, 4, 5, 4, 4, 5, 9, 5, 4] Most frequent number is: 4 

  Method # 4: Using collections.Counter.most_common()
A lesser known method to accomplish this particular task, Counter () uses the most_common function to accomplish this in one line. This is an innovative and different way to achieve this.

# Python3 demo code
# to get most common element
# using collection.Counter.most_common ()

from collections import Counter

 
# initializing list

test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]

 
# print original list

print ( "Original list:" + str (test_list))

 
# using most_common for
# get the most frequent element

test_list = Counter (test_list)

res = test_list.most _common ( 1 ) [ 0 ] [ 0 ]

 
# print result

print ( " Most frequent number is: " + str (res))

Output:

 Original list: [9, 4, 5, 4, 4, 5, 9, 5, 4] Most frequent number is: 4 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically