How to find the first most frequent element in list with Python

1 Answer

0 votes
from collections import Counter

lst = [4, 2, 6, 5, 5, 8, 3, 2, 1, 2, 2, 5, 5, 5, 5, 8, 9, 8, 8, 5, 5, 5, 8, 8, 8, 8, 8]

c = Counter(lst)

# 8 appear 9 times, 5 appear 9 times

print(c.most_common(1)[0])
print(c.most_common(1)[0][0])
print(c.most_common(1)[0][1])




'''
run

(5, 9)
5
9

'''

 



answered Feb 10, 2024 by avibootz
edited Feb 11, 2024 by avibootz

Related questions

...