Python min() Function


Python min() is a built-in function that returns the minimum value among the arguments supplied to the function. If an iterable is passed as the argument, the smallest item in the iterable is returned.

python min() function

Python min() Syntax

min() for numbers

min (num1, num2, *args[, key])

Where,

  • num1 (required) – first object for comparison
  • num2 (required) – second object for comparison
  • *args (optional) – any object for the comparison
  • key (optional) – key function to which each argument is passed and the comparison is done based on the value returned by this function

There can be any number of objects supplied as arguments for comparison.

Jump directly to the example

min() for iterables

min(iterable, *[, key, default])

Where,

  • iterable – sequence or collection whose smallest item is to be returned.
  • key (optional) – It is a function to which iterables are passed and the comparison is done based on the value returned by this function.
  • default (optional) – default value that specifies an object to return in case of empty iterable. If a default value is not given when empty iterable is passed, ValueError will be raised.

When an iterable is passed as the argument, the smallest item of the iterable is returned. If multiple iterables are passed then, the list for which the key function return smaller value is returned.

Jump directly to the example

Python min() Example

Example 1: Find minimum among the given numbers

>>> min(1,2,3,4)
1

>>> py_num = [3,5,7,9]
>>> min(py_num)
3

Example 1: Find number having minimum remainder when divided by 10 using key function

def findMin(num):
   rem = 0
   while(num):
     rem = num%10
     return rem

# using min(arg1, arg2, *args, key)
print('Number with min remainder is:', min(11,48,33,17,19, key=findMin))

# using min(iterable, key)
num = [12,48,33,17]
print('Number with min remainder is:', min(num, key=findMin))

When executed, this will yield following output.

Number with min remainder is: 11
Number with min remainder is: 42