How to find the most frequent value in a list with Python

2 Answers

0 votes
lst = [2, 5, 3, 3, 5, 1, 5, 7, 5, 5, 3, 6]
 
most_frequent_element = max(set(lst), key = lst.count)
     
print("most frequent element = %d\n" % most_frequent_element, end="", sep="")
 
 
 
'''
run:
 
most frequent element = 5
 
'''

 



answered Apr 8, 2019 by avibootz
edited Apr 21, 2023 by avibootz
0 votes
from statistics import mode

lst = [2, 5, 3, 3, 5, 1, 5, 7, 5, 5, 3, 6]

most_frequent_element = mode(lst)
    
print("most frequent element = %d\n" % most_frequent_element, end="", sep="")



'''
run:

most frequent element = 5

'''

 



answered Apr 21, 2023 by avibootz

Related questions

...