Answers for "python find the most common element in a list"

3

python find most occuring element

from collections import Counter

a = [1936, 2401, 2916, 4761, 9216, 9216, 9604, 9801] 

c = Counter(a)

print(c.most_common(1)) # the one most common element... 2 would mean the 2 most common
[(9216, 2)] # a set containing the element, and it's count in 'a'
Posted by: Guest on July-07-2020
0

Python | Find most frequent element in a list

# Program to find most frequent 
# element in a list
def most_frequent(List):
    return max(set(List), key = List.count)
  
List = [2, 1, 2, 2, 1, 3]
print(most_frequent(List))
Posted by: Guest on July-11-2021
-1

get most common element in list python

from collections import Counter

listOfArtists = [
 'Linkin Park',
 'Lil Peep',
 'Lil Peep',
 'Cold Hart',
 'Team Astro',
 'Tom Odell',
 'Bazzi',
 'The Weeknd',
 'Juice WRLD',
 'The Weeknd',
 'Foster The People',
 'Linkin Park',
 'Linkin Park',
 'Lil Peep',] 

amount = 4
print(Counter(listOfArtists).most_common(amount)) # amount = elements returned 

#output: [('Linkin Park', 3), ('Lil Peep', 3), ('The Weeknd', 2), ('Cold Hart', 1)]
Posted by: Guest on October-13-2021

Code answers related to "python find the most common element in a list"

Python Answers by Framework

Browse Popular Code Answers by Language