0
dictionary={}
list=[1,1,2,3,3,4,5]
maximum=0
for values in list:
   if values in dictionary:
      dictionary[values]+=1
   else:
      dictionary[values]=1
if not maximum or dictionary[values]>maximum[0]:
maximum=(values,dictionary[values])
mode=maximum[0]
print("Mode:",mode)

Output:3

The output should be 1 and 3 since both occur twice.

1
  • 2
    collections.Counter([1,1,2,3,3,4,5]) Mar 20, 2015 at 4:10

4 Answers 4

3

You are basically reinventing the built-in collections.Counter.

In [3]: my_list = [1, 1, 2, 3, 3, 4, 5]

In [4]: from collections import Counter

In [5]: counter = Counter(my_list)

In [6]: max_count = max(counter.values())

In [7]: mode = [k for k,v in counter.items() if v == max_count]

In [8]: mode
Out[8]: [1, 3]

Edit:

python 3 does support statistics.mode; however, an error is raised on your list, since no unique mode exists.

1
  • This comment saved me. Thanks for especially pointing out that statistics.mode throws an error when no unique mode exists. Nov 22, 2018 at 2:12
1

You can use scipy to get mode

>>> from scipy.stats import mode

>>> mode(alist)
1
# Function to return all modes, this function takes into account multimodal distributions.
# Function returns all modes as list or 'NA' if such value doesn't exist.
def mode(l):
    if len(l) > 1: #
        #Creates dictionary of values in l and their count
        d = {}
        for value in l:
            if value not in d:
                d[value] = 1
            else:
                d[value] += 1

        if len(d) == 1:
            return [value]
        else:
            # Finds most common value
            i = 0
            for value in d:
                if i < d[value]:
                    i = d[value]

            # All values with greatest number of occurrences can be a mode if:
            # other values with less number of occurrences exist
            modes = []
            counter = 0
            for value in d:
                if d[value] == i:
                    mode = (value, i)
                    modes.append(mode)
                    counter += mode[1] # Create the counter that sums the number of most common occurrences

            # Example [1, 2, 2, 3, 3]
            # 2 appears twice, 3 appears twice, [2, 3] are a mode
            # because sum of counter for them: 2+2 != 5
            if counter != len(l):
                return [mode[0] for mode in modes]
            else:
                return 'NA'
    else:
        return l

l1 = [1]
l2 = [1, 1]
l3 = [1, 1, 2, 2]
l4 = [1, 2, 3, 4, 5]
l5 = [1, 1, 2, 2, 3, 3, 4]
l6 = [1, 2, 3, 4, 4]
l7 = ['string', 'string', 1]
l8 = ['string', 'string', 1, 1, 1]

print mode(l1)
print mode(l2)
print mode(l3)
print mode(l4)
print mode(l5)
print mode(l6)
print mode(l7)
print mode(l8)
0

Another way of finding mode

def mode(lst):
    d = {}
    for a in lst:
        if not a in d:
            d[a]=1
        else:
            d[a]+=1
    return [k for k,v in d.items() if v==max(d.values())]
1
  • 1
    Please describe your code when providing an answer with a few comments.
    – BenT
    Aug 2, 2019 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.