min() function in python is used to find

  1. the smallest value of two or more numbers passed as arguments.
  2. lexicographically smallest value if strings are passed as arguments.
  3. the samllest item an any iterable like list, set, etc.

min() Syntax and Parameters

In general min() function has two types of syntax. We can either pass multiple values individually as separate arguments or whole combined as an iterable.

If multiples values are passed then the smallest of them will be returned.

Python min() function parameters
ParameterConditionDescription
val1,val2,val3…RequiredValues that need to be compared
keyOptionalA function specify custom comparison criteria to find minimum value.
Default value is None.

If an iterable is passed as an argument then the smallest value in the iterable is returned.

Python min() function parameters
ParameterConditionDescription
iterableRequiredAn iterable with one or multiple items
keyOptionalA function specify custom comparison criteria to find minimum value.
Default value is None.
defaultOptionalA value to return if the iterable is empty.
Default value is False.

let see some examples to better understand min() funcition.

Examples

Passing multiple values separately to the min() function.

Passing an iterable i.e list as an argument.

Note: If you pass an empty iterable than ValueError exception is raised.

So always provide default value as an argument in such cases.

Using len as key parameter to get shortest string.

We can also use user-defined lambda function as key to get desired value as the min result.

Hope this much examples helps you to know the basic functionality of min function. If you have any doubts or suggestions then please comment below.

Leave a Reply