Python min() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

4
a

Customizing the sort order

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

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

GfG

Exception Raised

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

Example:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1
3

Exception Raised

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

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

ValueError: min() arg is an empty sequence



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.