Skip to content
Related Articles

Related Articles

Python min() Function

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 Aug, 2020

Python min() function returns the smallest of the value or smallest item in an iterable passed as its parameter. There are two types of min function –

  • min() functions with objects
  • min() functions with an iterable

min() functions with objects

Unlike the min() function of C/C++, the min() function in Python can take any type of object and returns the smallest among them. In the case of strings, it returns the lexicographically smallest value.

Syntax: min(a, b, c, …, key=func)

Parameters:

a, b, c, .. : similar type of data.

key: A function to customize the sort order

Example:

Python3




# Python code to demonstrate the 
# working of min()
  
# printing the minimum of
# 4, 12, 43.3, 19, 100
print(min(4, 12, 43.3, 19, 100))
  
# printing the minimum of 
# a, b, c, d, e
print(min('a', 'b', 'c', 'd', 'e'))


Output:

4
a

Customizing the sort order

To customize the sort order key parameter is passed in the min() function. 

Example:

Python3




# Python code to demonstrate the 
# working of min()  
  
  
# find the string with minimum 
# length
s = min("GfG", "Geeks", "GeeksWorld", key = len)
print(s)


Output:

GfG

Exception Raised

min() functions throw TypeError when conflicting data types are compared.

Example:

Python3




# Python code to demonstrate the
# Exception of min() 
    
# printing the minimum of 4, 12, 43.3, 19, 
# "GeeksforGeeks" Throws Exception 
print(min(4, 12, 43.3, 19, "GeeksforGeeks"))


Output:

TypeError: unorderable types: str() < int()

min() functions with an iterable

When an iterable is passed to the min function it returns the smallest item of the iterable.

Syntax: min(iterable, default = obj, key = func)

Parameters:

iterable: An iterable like list, tuple, string

default: Default value that is returned when the iterable is empty

key: A function to customize the sort order

Example:

Python3




# Python code to demonstrate the
# working of min() 
    
# printing the minimum of [4, 12, 43.3, 19]
print(min([4, 12, 43.3, 19]))
  
# printing the minimum of "GeeksforGeeks"
print(min("GeeksforGeeks"))
  
# printing the minimum of ("A", "b", "C")
print(min(("A", "a", "C")))


Output:

4
G
A

Customizing the sort order

As seen above, to customize the sort order key parameter is passed in the min() function. 

Example:

Python3




# Python code to demonstrate the
# working of min() 
    
      
d = {1: "c", 2: "b", 3: "a"}
  
# printing the minimum key of
# dictionary
print(min(d))
  
# printing the key with minimum 
# value in dictionary
print(min(d, key = lambda k: d[k]))


Output:

1
3

Exception Raised

ValueError is raised when an empty iterable is passed without the default argument

Example:

Python3




# Python code to demonstrate the
# Exception of min() 
    
L = []
  
# printing the minimum empty list
print(min(L))


Output:

ValueError: min() arg is an empty sequence

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!