Online Tutorials & Training Materials |
Register Login

Use of Min() Function in Python

13 Sep 2019 12:04 pm || 0

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

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

If you want to find the largest element, use max() method.

Syntax : 


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.

Returns Error when conflict with arguments passed.

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


# 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"))


The Minimum is:  2
The Minimum is:  Apple

min () Function with Array / List

# 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))


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

min () Function with Tuple

# 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))


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

min () Function with Dictionary 

# 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))


The minimum number in dictionary:  1