Python min() Function Tutorial

In this section, we will learn what the min() function is and how to use it in Python.

min() Function in Python

The Python min() function is used to get the minimum element of a sequence of values or the minimum element of an iterable object.

Python min() Function Syntax:

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

Python min() Function Parameters

The min function takes three types of arguments:

`arg1,arg2,argN`: first of all, we can pass a sequence of values of the same type (for example a bunch of numbers or a bunch of string values etc.) As the argument of this function and it will calculate the minimum value in that sequence and return it as a result.

Note: if we passed a sequence of elements as the arguments of this function (for example, a set of numbers) then all the elements in the sequence should be of the same data type. Otherwise we will get an error. There’s an exception to this, however, and it is explained in just a second.

`iterableObject`: Other than a sequence of values, we can pass an iterable object (like a list, dictionary, set, or tuple) as the argument of the function and get the minimum value as a result. Note that the elements of the target iterable object should all be of the same type! Otherwise we will get an error. Note that there’s an exception to this, and it is explained in just a second.

`key`: The `min` function is capable of calculating the minimum values for basic data types like `numbers` and `strings`. But when it comes to complicated objects like a dictionary, for example, this function doesn’t know what to do and based on what value it should calculate the minimum element. So for this reason, the function provided another parameter called `key`. This parameter takes a reference to a function and will call the function per each element (the sequence of values or the elements of the iterable object that we pass to the function). Within the body of the target function, we need to return a basic data type value per each incoming element so that the `max` function uses those returned values and then calculates the minimum element to get the final result.

Also, using the `key` function allows us to pass an iterable object or a sequence of values that has elements with different data types. But remember, it is now our responsibility to make sure the returned values of the `key` function are of the same data type.

Note: the `key` function is optional.

Python min() Function Return Value

The return value of this function is the minimum element of the target iterable object or the sequence of elements we’ve passed to the function.

Example: getting the smallest number

result = min (1,2,4,7,34,3,2,645,64,342,34,34645,34,523,34,332,34,364,5634,5234,2343,4645,342)

print (result)

Output:

1

Example: python min of list

list1 = [1,2,3,4,"five"]


def func(element):
    if type(element) ==str:
        return 10 
    else:
        return element


result = min(list1, key = func)
print (result)

Output:

1

Example: the smallest string in a list

list1 = ["Jack","Elon","Ellen","Omid"]

result = min(list1)

print (result)

Output:

Ellen

Example: python min function with dictionaries

list1 = [
    {"name":"Omid","age":5000},
    {"name":"John","age": 7000},
    {"name":"Elon","age":400},
    {"name":"Ellen", "age":6000}
]


def func(element):
    return element["age"]


result = min(list1 , key = func)
print (result)

Output:

{'name': 'Elon', 'age': 400}
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies