min(arg1,arg2, key=my_fun )
arg1, arg2 :numbers , iterable inputs or more than one inputs
key=my_fun : built-in functions or user defined functions based on which the minimum value can be returned.
Return value is the lowest value or the list with lowest positional element (or lowest based on the input function).
Using numbers
x=6
y=9
print(min(x,y)) # 6
Using list
my_list=[6,1,2,3,4,5]
print(min(my_list)) # 1
Using string
min() returns based on alphabetical order.
my_list=['Hello','Welcome','To','Plus2net']
print(min(my_list)) # Hello
my_list=['hello','Welcome','to','plus2net']
print(min(my_list)) # Welcome
Using Dictionary
Output depends on Keys of the Dictionary ( Not Values ).
my_dict={1:'A',2:'B',3:'C'}
print(min(my_dict)) # 1
Getting lowest t key value pair based on value of the dictionary
Student Name and mark is stored in a dictionary, collect the student who got the lowest mark.
my_dict={'Raju':45,'Ronn':56,'Tom':50}
key_of_min_value = min(my_dict, key=my_dict.get)
print(key_of_min_value,my_dict[key_of_min_value])
Output is here
Raju 45
We will use tuple
my_tuple=(6,1,12,7)
print(min(my_tuple)) #1
Using more than one list
my_list1=[5,1,1]
my_list2=[6,1,-3]
print(min(my_list2,my_list1)) # [5, 1, 1]
More than two lists
my_list1=[4,7,8,9]
my_list2=[5,1,1]
my_list3=[6,1,-3]
print(min(my_list2,my_list1,my_list3)) # [4, 7, 8, 9]
Here comparison is done from left to right and based on the position of the first lowest element the list is returned ( by min() ) as minimum.
Check the below code, since first element is equal in all three lists , the comparison is done based on the 2nd element ( 1, equal in my_list2 and my_list3) and finally based on the third element my_list3 is returned as lowest.
my_list1=[4,7,8,9]
my_list2=[4,1,1]
my_list3=[4,1,-3]
print(min(my_list2,my_list1,my_list3)) # [4, 1, -3]
Using the key
We can use any built-in function like sum or len to get the list with minimum value.
my_list1=[4,2,3,7,8]
my_list2=[5,1,50]
print(min(my_list2,my_list1,key=sum)) # [4, 2, 3, 7, 8]
Using Len
my_list1=[4,2,3,7,8]
my_list2=[5,1,50]
print(min(my_list2,my_list1,key=len)) # [5, 1, 50]
Based on UDF
We will use one user defined function to find out the last number of the input list and based on this last number we can get the list with highest number.
def last_digit(my_list):
return my_list[-1] # last digit
my_list1=[4,2,3,7,8,9]
my_list2=[25,41,1]
print(min(my_list2,my_list1,key=last_digit)) #[25, 41, 1]
« max()
Iterator »
any() »
← Subscribe to our YouTube Channel here