Online Tutorials & Training Materials | STechies.com
Register Login

Use of Min() Function in Python

13 Sep 2019 12:04 pm || 0

While programming in Python, it is common to work with data structures such as lists and tuples. If you perform operations using elements of a list, it is possible that you will have to determine the highest or lowest value among the elements. For this, Python offers two methods – max() and min().   

What is Python min()?

The Python min() function is used to find the smallest element of iterable or two or more arguments.

min() function can be used with List / Array, Tuple, Sets & Dictionary 

Syntax 

min(a,b,c,..)
min(iterable, *[, key, default])

Parameters 

 

  • x,y,z… (required): multiple items to compare
  • Iterable : (required) string, list, tuple etc 

Return Value 

Returns the minimum of all the arguments.

Exceptions

Returns Error when conflict with arguments passed.

key (optional): Key is the name of the function to which arguments or iterable is passed and the comparison is done based on the value returned by this function.

Example

# Python code to explain min() function 

# Find lowest integers value 
print('The Minimum is: ',min(44, 2, 34, 12))

# Find minimum character in arguments
print('The Minimum is: ',min("Apple", "banana", "greps"))

Output: 

The Minimum is:  2
The Minimum is:  Apple

Explanation

As per the output, the minimum value among the numbers is 2, determined by the min() method. The next line of the output is Apple. This string is considered the minimum element by min(). This is because the min() method determines the smallest string according to its alphabetical order. As Apple comes first, it is the smallest.       

min() Function with Array / List

Example

# Python code to explain min() function with list/array

# Declare integer list
listInt = [1, 33, 2, 23, 7, -4, 22, 222, 44, 232, 2]

# Declare string list
listText = ["Apple", "Banana", "Greps", "Orange","Pears"]

# Find lowest number in list
print('The Lowest number in list: ', min(listInt))

# Find Lowest character in list without key function 
print('The Lowest : ', min(listText))

# Find lowest character in list with key function
print('The Lowest length of element in list: ', min(listText, key=len))

Output

The Lowest number in list:  -4
The Lowest :  Apple
The Lowest length of element in list:  Apple

Explanation

Let us understand a little more about this program. The min() method finds out the minimum number among the list of integers of listInt. The result is -4, as it is a negative number. Then the smallest string among the elements of the listText, is printed, that is Apple. This is because Apple comes first alphabetically among the other strings.

The last line of the output displays the string with the lowest length, that is Apple. The min() method determines this with the help of an optional argument called key=len that finds out the length of the string. Here, both Apple and Pears have a length of 5. But as Apple comes first alphabetically, it is considered the string with the lowest length.     

min() Function with Tuple

Example

# Python code to explain min() function with tuple

# Declare integer tuple
listtuple = (1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2)

# Declare string tuple
listtuple = ("Apple", "Banana", "Greps", "Orange","Pears")

# Find lowest number in tuple
print('The Minimum number in tuple: ', min(listtuple))

# Find lowest character in tuple without key function 
print('The Minimum : ', min(listtuple))

# Find minimum character in tuple with key function
print('The Minimum length of element in tuple: ', min(listtuple, key=len))

Output

The Minimum number in tuple:  Apple
The Minimum :  Apple
The Minimum length of element in tuple:  Apple

Explanation

In this code, the minimum value of listtuple is displayed to be Apple. This is because Apple starts with the letter “A” that appears first alphabetically. But determining the string with the minimum length, the argument key=len is passed to the min() method. So, first, the min() checks the length of the strings. In this list listtuple, the strings “Apple”, “Greps” and “Pears” have 5 characters each. But the min() method decides the string “Apple” to be the minimum as it starts with “A” which comes first according to the alphabetical order.  

min() Function with Dictionary 

Example

# Python code to explain min() function with dictionary

# Declare dictionary
listdis = {1:"Apple", 3:"Banana", 2:"Greps", 4:"Orange", 7:"Pears"}

# Find minimum in dictionary
print('The minimum number in dictionary: ', min(listdis))

Output

The minimum number in dictionary:  1

Explanation

If we look at this code, we can see 5 key-value pairs inside the dictionary. While determining the lowest number among such key-value pairs, the min() method checks the keys and ignores the values. The smallest key will be selected as the minimum value. So the method selects 1 as the minimum number in the dictionary. 

Conclusion

The key parameter is optional, so make sure to specify it properly. While determining the minimum element form a dictionary, make sure there are no syntax errors and the key-value pairs are coded correctly.