Python min function

In this post, we will see about python min function.
Python min() function returns min value in iteable or passed arguments.



It returns min value from iterable or passed arguments


Let’s see a very simple example:

#min in the list l=[8,2,13,6,9] print("Min in list:",min(l)) #Min in list of String lStr=['John','Adam','Martin','Sam','Mary'] print("Min in list of String:",min(lStr)) #Min in tuple t=(10,32,34,25,7) print("Min in tuple:",min(t)) #Min in arguments print("Min among 25,27:",min(25,27)) print("Min among 38,26,28:",min(38,26,28)) #If different type of argument in the list l=[30,1,34,'Apple','Orange'] print(min(l))


Min in list: 2
Min in list of String: Adam
Min in tuple: 7
Min among 25,27: 25
Min among 38,26,28: 26
TypeError Traceback (most recent call last)
in ()
17 #If different type of argument in the list
18 l=[30,1,34,’Apple’,’Orange’]
—> 19 print(min(l))

TypeError: ‘<' not supported between instances of 'str' and 'int'

As you can see if we have different data types in the list, it raises TypeError.
Let’s see how can we use key attribute in min function.

def checkLen(num): return len(str(num)) # It will return min number of digits print(min(134,26,12321,1110,key=checkLen)) # It will return list with min length l1=[54,34,23] l2=[10,34,5,6] l3=[45,3,9,6,5] print(min(l1,l2,l3,key=len))


[54, 34, 23]
In [42]:

That’s all about python min function.

Leave a Reply

Your email address will not be published. Required fields are marked *