Python min() function

Updated on Jan 07, 2020


The min() function returns the smallest of the input values.

Its syntax is as follows:

min(iterable[, default=obj, key=func]) -> value
PARAMETER         DESCRIPTION
iterable (required) An iterable object like string, list, tuple etc.
default (optional) The default value to return if the iterable is empty.
key (optional) It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable.

or

min(a, b, c, ...[, key=func]) -> value
PARAMETER      DESCRIPTION
a, b, c ... Items to compare
key (optional) It refers to the single argument function to customize the sort order. The function is applied on each of the items.

If min() is called with an iterable, it returns the smallest item in it. If the iterable is empty then the default value is returned (assuming it is provided), otherwise a ValueError exception is raised.

If min() is called with multiple arguments, it returns the smallest one.

Let's see some examples:

Example 1: Calling min() with an iterable

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
>>> 
>>> min("abcDEF") # find smallest item in the string
'D'
>>>
>>> 
>>> min([2, -1, 4, 3]) # find smallest item in the list
-1
>>> 
>>>
>>> min(("one", "two", "three")) # find smallest item in the tuple
'one'
>>> 
>>> 
>>> min({1: "one", 2: "two", 3: "three"}) # find smallest item in the dict
1
>>>
>>>
>>> min([]) # empty iterable causes ValueError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence
>>> 
>>> 
>>> min([], default=0) # supressing the error with default value
0
>>>

Try it out:

# find smallest item in the string
print(min("abcDEF")) 

# find smallest item in the list
print(min([2, -1, 4, 3])) 

# find smallest item in the tuple
print(min(("one", "two", "three"))) 
 
# find smallest item in the dict
print(min({1: "one", 2: "two", 3: "three"}))

#print(min([]))  # empty iterable causes ValueError

# supressing the error with default value
print(min([], default=0))


Example 2: Calling min() with multiple arguments

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> 
>>> min(20, 10, 30, -5) 
-5
>>>
>>>
>>> min("c", "b", "a", "Y", "Z")
'Y'
>>>
>>> 
>>> min(3.14, -9.91, 2.41)
-9.91
>>>

Try it out:

print(min(20, 10, 30, -5))

print(min("c", "b", "a", "Y", "Z"))
 
print(min(3.14, -9.91, 2.41))


Trying to find largest value among the objects of different types causes an error.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> 
>>> min(10, "pypi")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
>>> 
>>>
>>> min(5, [-10, 55])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: list() > int()
>>>

Customing Sort Order #


To customize the sort order we use the key named argument. It works exactly like the key named argument of the sorted() function.

Here is an example where we use key argument to make the string comparision case-insentive.

1
2
3
4
5
6
7
>>>
>>> min("c", "b", "a", "Y", "Z")
'Y'
>>> 
>>> min("c", "b", "a", "Y", "Z", key=str.lower)
'a'
>>>

Try it out:

print(min("c", "b", "a", "Y", "Z"))

print(min("c", "b", "a", "Y", "Z", key=str.lower))


The following is another example where we compare strings based on its length instead of their ASCII values.

1
2
3
4
5
6
7
>>> 
>>> min(("java", "python", "z++"))
'java'
>>> 
>>> min(("java", "python", "z++"), key=len)
'z++'
>>>

Try it out:

print(min(("java", "python", "z++")))

print(min(("java", "python", "z++"), key=len))


There also exists a complementary function called max() which finds the largest of the input values.


Other Tutorials (Sponsors)