Python Reference ยท Built-in Functions
Python min() function
Reference for Python min(): find the smallest item of an iterable or among arguments, use key and default, compare strings and handle empty input.
The built-in min() function returns the smallest item. You can give it one iterable (a list, tuple, set, string, generator...) or two or more separate arguments. Items are compared with <, so they must be comparable with each other.
Pass a key function to choose what is compared, for example the length of a string or one field of a record. min() then returns the original item, not the key. If several items tie for the smallest, the first one wins.
min([4, 2, 9]) # 2 min(7, 3, 5) # 3 min(["pear", "fig"], key=len) # "fig"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
min(iterable, *, key=None) min(iterable, *, default, key=None) min(arg1, arg2, *args, key=None)
| Name | Description |
|---|---|
iterable |
Any iterable of comparable items. |
arg1, arg2, *args |
Two or more values to compare when you do not pass an iterable. |
key |
Optional one-argument function. Each item is passed to it and the results are compared. |
default |
Optional, keyword-only, and only valid with a single iterable. Returned when the iterable is empty. |
Return value: the smallest item (the first one if several tie).
Raises: ValueError if the iterable is empty and no default was given. TypeError if items cannot be ordered against each other (for example min(1, "a")) or if you give no arguments or a non-iterable single argument.
Usage notes
keychanges the comparison, not the result.min(words, key=len)returns a word, not a length.- Ties go to the first item. That makes results predictable, and it is why
min(people, key=...)above returns Ben, not Cy. defaultonly helps with an empty iterable. It is not a lower bound. Usemin(x, 0)for "at most zero" andmin(values, default=0)for "smallest, or 0 if there is nothing".- Dictionaries iterate over keys. To find the smallest value use
min(d.values()); to find the key with the smallest value usemin(d, key=d.get). - Iterators are consumed. Passing a generator uses it up; you cannot loop over it again afterwards.
- Strings compare by code point, so
"Zebra" < "apple"is true. Usekey=str.lowerfor case-insensitive comparison. - Mixed types such as
intandfloatwork;strandintdo not. NaNis a trap. Comparisons withfloat("nan")are always false, so the result depends on the order of items.- For the smallest and largest in one pass, loop once yourself or use
max()separately; for the n smallest useheapq.nsmallest.
Related
max(),sorted(),sum(),abs()- Lessons: Math and random, Higher-order functions, Lists