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

The first line print('The Minimum is: ', min(44, 2, 34, 12)), checks the minimum value among the numbers. As the lowest number among the mentioned numbers is 2, it is printed to the screen. The second line checks the smallest string among the ones mentioned. Among the strings "Apple", "banana", "greps"; the string which alphabetically comes first is Apple. So, the min() method considers this string as the minimum. The string Apple is printed to the screen as a result.

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

Here, an integer list called listInt is initialized with the elements [1, 33, 2, 23, 7, -4, 22, 222, 44, 232, 2]. Then a string list called listText is initialized with the elements ["Apple", "Banana", "Grapes", "Orange","Pears"]. The next line of code, print('The Lowest number in list: ', min(listInt)) prints out the lowest number in the first list of integers. Similarly, the lowest string is printed out using the min() method in the next line.

The last line prints the lowest element in the list with the help of a key function that specifies len or length. After program execution, the lowest number -4 is printed at first. Then, the minimum string element Apple is displayed to the user.

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 program, an integer tuple called listtuple is created using the elements (1, 32, 2, 22, 7, -3, 232, 223, 12, 317, 2). Then the tuple is assigned values of strings ("Apple", "Banana", "Greps", "Orange", "Pears"). In the next line, with the help of a min() method that takes in an argument listtuple, the lowest element of the list is determined. This value is printed out using the print statement.

Then the string elements with the minimum length is printed out in the next statement using the min() method that has a key value of len (length). The final result is the string with the lowest length, Apple. The other print statements also print out Apple as it is the minimum element. 

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

In this program, a dictionary called listdis is initialized with keys and value pairs of {1:"Apple", 3:"Banana", 2:"Greps", 4:"Orange", 7:"Pears"}. The next print statement displays the minimum value of the dictionary list, by passing the listdis dictionary as argument to the min() method. The min() method checks the lowest key among the key-value pairs within the dictionary. Hence, the lowest key 1 is printed as the final result. 

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.