Skip to content

Min Function in Python (With Examples)

Min function in Python

Min function in Python returns the smallest item from an iterable or from two and more arguments .

Syntax of min function in Python

min(collection[,key])

Parameters of min function in Python

  • collection = collection is the list of objects separated by comma or any iterable .
  • key = Optional . It is a one argument function that will be applied to every object of the collection or to every item in the iterable . Must be in keyword form .

Return type of min function in Python

It returns the the largest item from an iterable or from two and more arguments .

Examples of min function in Python

Example 1: Get the minimum number from a list of numbers

# Get the minimum number from a list of numbers
min_num=min(10,5,16,34,6)
print(min_num)
5

In the above code we have find the minimum number from the given numbers using min() function . Here as collection parameter we have used a list of objects separated by comma .

Example 2: Get the smallest item from an iterable

# get the smallest item from a list
min_item = min([10,5,16,34,6])
print("min item from the list is =",min_item)

# get the smallest item from a tuple
min_item = min((10,5,16,34,6))
print("min item from the tuple is =",min_item)

# get the smallest item from a set
min_item = min({10,5,16,34,6})
print("min item from the set is =",min_item)

# get the smallest item from a dictionary
min_item = min({10:1,5:2,16:3,34:4,6:5})
print("min item from the dictionary is =",min_item)

# get the smallest item from a string
min_item = min("32576")
print("min item from the string is =",min_item)
min item from the list is = 5
min item from the tuple is = 5
min item from the set is = 5
min item from the dictionary is = 5
min item from the string is = 2

In the above code we have got smallest item from various iterable using min() function .

Example 3: Min function with key parameter

# getting the smallest item from a list
# without using key parameter
min_item=min(["2","11","22","3"])
print("min item without key parameter =",min_item)

# getting the smallest item from a list
# using key parameter
min_item=min(["2","11","22","3"],key=int)
print("min item with key parameter =",min_item)
min item without key parameter = 11
min item with key parameter = 2
  • In the above code first we have got the smallest element from the list using min() function without key parameter . As "11" is the smallest item so it returned "11" .
  • Then we have used min() function with key parameter to get the smallest element from the list . We have used int() function as key parameter . We do not need to call the function we only need to mention the function name . As a result all the string of the list is converted to integer . Now 3 is the smallest integer in the list . So the program returned 3 .

Example 4: Get the smallest pair from a list whose multiplication is the lowest

# getting the smallest pair from a list
# whose multiplication is the lowest
# defining the key function
def multiply(mytuple):
    multiplication=1
    for no in mytuple:
        multiplication*=no
    return multiplication    

min_pair=min([(5,4),(6,3),(7,2)],key=multiply)
print("min pair is =",min_pair)
min pair is = (7, 2)
  • In the above code we have defined a function multiply which will take a tuple of numbers and muliply the numbers and return their multiplication .
  • Then we have used this function as the key parameter of min() function . Now multiply() function has converted (5,4) to 20 , (6,3) to 18 and (7,2) to 14 . As 14 is the largest number so program returned (7,2) .

Example 5: Use lambda expression as key parameter of min() function

# getting the smallest pair from a list
# whose multiplication is the lowest

min_pair=min([(5,4),(6,3),(7,2)],key=lambda x:x[0]*x[1])
print("min pair is =",min_pair)
min pair is = (7, 2)

In the above code we have solved the previous problem using lambda expression as key parameter .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply