1

In Python How to print smallest number from list when mode is not available

I Would like to find Mode in python array or list, but if all numbers appears at only once(or we can say there is no Mode) I wanted to print smallest number.

n_num = [64630, 11735, 14216, 99233, 14470, 4978, 73429, 38120, 51135, 67060]

from statistics import mode


def mode(n_num):
            n_num.sort()
            m = min(n_num)
            return m
print(str(mode(n_num)))

Submitted May 16th 2021 by Admin

Answers
0

Python already has a min and max function built-in to find the smallest and largest values in a list

n = [64630, 11735, 14216, 99233, 14470, 4978, 73429, 38120, 51135, 67060] smallest_number = min(n)
largest_number = max(n)

Admin | 4 months ago


0

You can use multimode() from the statistics package instead of mode(). This will return multiple values when there is more than one mode to choose from. You can take the min() from that:

from statistics import multimode n_num = [10, 9, 1, 2, 3, 4]
min(multimode(n_num))
# 1 n_num = [10, 9, 1, 2, 3, 4, 9, 10 ]
min(multimode(n_num))
#9

Admin | 4 months ago



Relevant Questions