Python min()

The min() method returns the smallest element in an iterable or smallest of two or more parameters.

Differnt syntaxes of min() are:

min(iterable, *iterables[,key, default])
min(arg1, arg2, *args[, key])

If you want to find the largest element, use max() method.


min() Parameters

min() has two forms of arguments it can work with.

  1. min(iterable, *iterables[, key, default])
    • iterable - sequence (tuple, string), collection (set, dictionary) or an iterator object whose smallest element is to be found
    • *iterables (Optional) - any number of iterables whose smallest is to be found
    • key (Optional) - key function where the iterables are passed and comparison is performed based on its return value
    • default (Optional) - default value if the given iterable is empty
  2. min(arg1, arg2, *args[, key])
    • arg1 - mandatory first object for comparison (could be number, string or other object)
    • arg2 - mandatory second object for comparison (could be number, string or other object)
    • *args​ (Optional) - other objects for comparison
    • key - key function where each argument is passed, and comparison is performed based on its return value

Return value from min()

The min() method returns:

1. min(iterable, *iterables[, key, default])

min() method return value for min(iterable, *iterables[, key, default])
Case Key Default Return value
Empty iterable   No Yes No Raises ValueError exception
Empty iterable Yes Yes Returns the default value
Single iterable (Not empty) No No Yes Returns the smallest among the iterable
Single iterable (Not empty) Yes No Yes Passes each element in the iterable to the key function Returns smallest element based on the return value of the key function
Multiple iterable (Not empty) No No Yes Returns smallest among the given iterables
Multiple iterable (Not empty) Yes No Yes Passes each iterable to the key function Returns smallest iterable based on the return value of the key function

2. min(arg1, arg2, *args[, key])

min() method return value for min(arg1, arg2, *args[, key])
Case Key Return value
First and second argument passed No   Returns smallest among the given arguments
First and second argument passed Yes Passes the arguments to the key function Returns smallest among the arguments based on the return value of the key function
More than 2 arguments passed No Returns smallest among the given arguments
More than 2 arguments passed Yes Passes each arugment to the key function Returns smallest among the arguments based on the return value of the key function

Example 1: Find minimum among the given numbers

# using min(arg1, arg2, *args)
print('Minimum is:', min(1, 3, 2, 5, 4))

# using min(iterable)
num = [3, 2, 8, 5, 10, 6]
print('Minimum is:', min(num))

When you run the program, the output will be:

Minimum is: 1
Minimum is: 2

Example 2: Find number whose sum of digits is smallest using key function

def sumDigit(num):
    sum = 0
    while(num):
        sum += num % 10
        num = int(num / 10)
    return sum

# using min(arg1, arg2, *args, key)
print('Minimum is:', min(100, 321, 267, 59, 40, key=sumDigit))

# using min(iterable, key)
num = [15, 300, 2700, 821, 52, 10, 6]
print('Minimum is:', min(num, key=sumDigit))

When you run the program, the output will be:

Minimum is: 100
Minimum is: 10

Here, each element in the passed argument (list or argument) is passed to the same function sumDigit().

Based on the return value of the sumDigit(), i.e. sum of the digits, the smallest is returned.


Example 3: Find list with minimum length using key function

num = [15, 300, 2700, 821]
num1 = [12, 2]
num2 = [34, 567, 78]

# using min(iterable, *iterables, key)
print('Minimum is:', min(num, num1, num2, key=len))

When you run the program, the output will be:

Minimum is: [12, 2]

In this program, each iterable num, num1 and num2 is passed to the built-in method len().

Based on the result, i.e. length of each list, the list with minimum length is returned.