0

So I have a list of values, and I have an idea of how to get the mode. But I'm unsure of how exactly to execute it. I know I will need the max value of the list using max() and also for x in list,and perhaps creating two lists, one for unique values and the other for how many times they appear, but besides that. I'm completely lost. My list is:

 [0,68, 92, 68, 49, 43, 68]

Expected output:

68

Sorry if this is vague! This is not a duplicate. I don't want to use a counter function or dictionary. Only lists/ loops.

1 Answers1

2

The mode of a set of data values is the value that appears most often. You can easily get this using python's Counter module's most_common method. It takes n that defines how many results you want, 1 is the most common 2 would be the two most common.

from collections import Counter
data = Counter( [0,68, 92, 68, 49, 43, 68])
print data.most_common(1) 
> [(68, 3)]

most_common: Return a list of the n most common elements and their counts from the most common to the least. If n is omitted or None, most_common() returns all elements in the counter. Elements with equal counts are ordered arbitrarily:

user1767754
  • 23,311
  • 18
  • 141
  • 164