Python 3 reference · Built-in Functions
Python min() Function
Return the smallest item in an iterable, or the smallest of several arguments. Supports a key function and a default.
Python 3 Since 3.0
Syntax
min(iterable, *, key=None, default)
min(arg1, arg2, *args, key=None)
Parameters
| Parameter | Description |
|---|---|
iterable / args |
One iterable, or two or more values. |
key optional |
A function that returns the value to compare. Default: None |
default optional |
Returned when the iterable is empty. |
Return value
The smallest item.
Examples
print(min(3, 9, 4))
print(min([2.5, 7.1, 0.3]))
Output
3 0.3
cities = {"Oslo": 7.2, "Cairo": 22.1, "Lima": 19.4}
print(min(cities, key=cities.get))
print(min(["pear", "fig", "banana"], key=len))
Output
Oslo fig
print(min([], default=None))
value = 140
print(max(0, min(value, 100))) # clamp to the range 0–100
Output
None 100
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
| Python 3.4 | Added the default parameter. |
| Python 3.8 | key can be None. |
Related
max()· Return the largest item in an iterable, or the largest of several arguments. Supports a key function and a default.sorted()· Return a new sorted list from any iterable. Supports a key function and reverse order; the original is left unchanged.sum()· Add up the numbers in an iterable, plus an optional start value. Floats are summed with extra precision since Python 3.12.- Python Lists · tutorial