Python

Python Min() Function

Python supported various inbuilt functions and modules to make code easier as well as faster by performing simple to complex tasks. Retrieving the smallest item or element from the given arguments object or iterable is considered an important task in Python. To accomplish this, the inbuilt β€œmin()” function is used in Python.

This guide covers a complete guide on Python’s β€œmin()” function utilizing multiple examples.

What is the min() Function in Python?

The β€œmin()” function is utilized in Python to determine the smallest item/element in an iterable or while using multiple objects.

Syntax

The syntax of the β€œmin()” function while using iterable:

min(iterable, *iterables, key, default)

Here, in this syntax, the β€œiterable” and β€œ*iterables” parameters signify any multiple iterable, such as a list, tuple, string, etc. The β€œkey” parameter specifies the function that is used to perform a comparison based on particular criteria.

The syntax of the β€œmin()” function while using multiple objects:

min(arg1, arg2, *args, key)

In this syntax, the β€œarg1”, β€œarg2”, and β€œ*args” indicate multiple objects.

Return Value

The β€œmin()” function retrieves the smallest item from multiple passed objects or any iterable.

Example 1: Determining the Smallest Element in a List

The smallest item in a list iterable can be determined by passing the list to the β€œmin()” function. The min() function retrieved the smallest positive and negative item in a list:

num = [23, 22, 18, 55, 10, 16]
print('Smallest Item: ',min(num))

num1 = [-23, -22, -18, -55, -10, -16]
print('\nSmallest Item: ',min(num1))

The smallest positive list item and negative list element have been retrieved successfully:

Example 2: Determining the Smallest Element from the Passed Arguments

Besides the iterables, the β€œmin()” function can also retrieve the smallest element of the passed arguments or objects. Here, the β€œmin()” function retrieves the positive and negative numbers from the passed multiple arguments:

print('Smallest Item: ',min(15, 10, 12, 20))
print('\nSmallest Item: ',min(-15, -5, -8, -15))

The smallest item of the multiple objects has been retrieved successfully:

Example 3: Determining the Smallest String Element in a List

The minimum or smallest value of the list of strings can also be determined using the β€œmin()” function. Here in the below, the β€œmin()” function retrieves the starting based on the smallest alphabetical orders:

str1 = ["Joseph", "Anna", "Lily", "Henry"]
print(min(str1))

The minimum or smallest string list element has been retrieved successfully:

Example 4: Determining the Smallest String Element in a List Based on the Key

We can also determine the minimum or smallest list string element according to the specified criteria using the β€œkey=” parameter. Here, the β€œkey=” parameter denotes the β€œlen” function that is used to retrieve the string list element based on the length of the string:

str1 = ["Joseph", "Zendaya", "Parker", "Henry"]
print(min(str1, key=len))

The list element with greater length has been retrieved to the output:

Example 5: Determining the Smallest Key in a Dictionary

The β€œmin()” function can also be used to determine the smallest key in a dictionary. Take the following code as an example:

dic1 = {5: 14, 10: 10, 2: 10, 12: 14}
print('Smallest Dictionary Key: ',min(dic1))

The above code retrieves the smallest key to the output:

Example 6: Determining the Smallest Value of the Dictionary Key

The key with the smallest values can also be retrieved to the output using the min() function along with the lambda function. The lambda function is passed to the key parameter that retrieves the second element (values) of the dictionary. After retrieving the second dictionary value, the min() function is applied to the dictionary to get the key that contains the smallest value:

dic1 = {5: 14, 10: 5, 2: 1, 12: 14}
print('Dictionary Key With Smallest Value: ',min(dic1, key = lambda k: dic1[k]))

The dictionary with the smallest key has been retrieved to the output:

Example 7: Determining the Smallest Value of the Empty Iterable

When the min() function is applied to the empty iterable, users often encounter the β€œValueError”. For example, the following code demonstrates this:

list1 = []
print('Smallest Value: ',min(list1))

The error will look like this:

To rectify this error, the β€œmin()” function takes the β€œdefault” argument with a β€œNone” value:

list1 = []
print('Smallest Value: ',min(list1, default=None))

The above code retrieves the β€œNone” default value because the iterable is empty:

Conclusion

In Python, the inbuilt β€œmin()” function retrieves the smallest item/element in an iterable such as a list, string, or dictionary. It can also be used to retrieve the smallest item from the passed argument object. We can determine the smallest element based on the specified key, such as the len function. This guide delivered a complete overview of the β€œmin()” function using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.