Find a minimum directly
Choose the least numeric or lexicographic value from a collection.
Python · Function
Returns the smallest item from an iterable or the smallest among multiple arguments. Use key for derived comparisons and default to handle an empty iterable.
With one positional argument, min() consumes an iterable; with multiple positional arguments, it compares those values directly. The optional key function supplies comparison values, default supplies a result for an empty iterable, and ties return the first minimum encountered.
min(iterable, /, *, key=None)
min(iterable, /, *, default, key=None)
min(arg1, arg2, /, *args, key=None)| Name | Type | Description |
|---|---|---|
iterable / arg1 | iterable or object | The iterable to search, or the first value in the multi-argument form. |
arg2, ...args | object | Additional values compared in the multi-argument form. |
key | callable or None | One-argument function that supplies ordering values. |
default | object | Fallback returned when the iterable is empty. |
The smallest item, or the smallest positional argument; with default, the fallback object for an empty iterable.
scores = {"Ada": 91, "Linus": 87, "Grace": 95}
lowest_name = min(scores, key=scores.get)
print(lowest_name) # LinusSelects the name whose mapped score is smallest.
Choose the least numeric or lexicographic value from a collection.
Select the object with the smallest field or computed score.
Avoid an exception when no candidates are available.
Resolve ties predictably while scanning ordered input.
With one positional argument, min() expects an iterable and returns its smallest item. With two or more positional arguments, it returns the smallest positional argument.
Follow-up: How can min() choose by a derived attribute? Pass a one-argument key function, such as key=len for shortest strings.
key receives each candidate and provides the value used for ordering. The candidate itself, rather than its key value, is returned.
Follow-up: What happens when key is omitted? Candidates are compared directly using their ordering behavior.
default is returned when the supplied iterable is empty. Without default in that situation, min() raises ValueError.
Follow-up: Does default apply when multiple positional arguments are supplied? No; that form compares arguments directly, while default belongs to the iterable form.
maxsortedlist.sortheapq.nsmallest