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:
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:
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:
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('\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:
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:
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:
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:
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:
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:
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.