Python min() Function
min(iterable) or min(a, b, ...) returns the smallest item; use key= to decide how items are compared and default= to avoid an error on empty input.min() finds the lowest score, the cheapest price, the earliest date or the shortest word without writing a loop. It accepts either one iterable (a list, tuple, set, string, dictionary) or several separate arguments. Its twin is max(), and for a total see sum().
By default items are compared with <, so numbers compare by value and strings compare alphabetically by character code.
| Syntax | min(iterable, *, key=None, default=<no default>) or min(arg1, arg2, *args, key=None) |
|---|---|
| Parameters | iterable or several arguments; key: function giving the value to compare; default: result for an empty iterable (keyword-only) |
| Returns | The smallest item itself (not its index) |
| Raises | ValueError on empty input without default; TypeError if items cannot be compared |
How min() compares numbers, strings and tuples
Numbers are compared by value. Strings are compared character by character using their Unicode code points, so uppercase letters come before lowercase ones and "Zebra" is smaller than "apple". Tuples compare their first elements, then the second on ties. When several items are equally small, min() returns the first one it meets.
print(min(7, 3, 9))
print(min([4.5, 2, 8]))
print(min("Zebra", "apple"))
print(min("python"))Using key= to find the shortest word or cheapest item
key takes a function that is applied to each item; the item with the smallest result wins, but the original item is returned. Use len for the shortest string, a lambda for fields of tuples or dictionaries, or dict.get to find the smallest value in a dictionary.
words = ["banana", "fig", "cherry"]
print(min(words, key=len))
prices = {"pen": 12, "book": 150, "bag": 600}
print(min(prices, key=prices.get))Empty sequences: the default argument
min([]) raises ValueError. When the list may be empty, give a fallback with default: min(scores, default=0). The default keyword works only with a single iterable, not with several positional arguments. The same behaviour applies to max().
Examples you can run
Press Run, then edit the code and run it again.
Lowest score and cheapest price
The basic uses with a list and with separate arguments.
scores = [72, 88, 95, 61]
print(min(scores))
print(min(12.5, 9.99, 15))Expected output61
9.99
Find the cheapest product with key
The key function picks the price, and the whole tuple is returned.
products = [("pen", 12), ("book", 150), ("ruler", 8)]
cheapest = min(products, key=lambda p: p[1])
print(cheapest)
print(cheapest[0])Expected output('ruler', 8)
ruler
Handle an empty list
default avoids a crash when there is no data.
scores = []
print(min(scores, default=None))
print(min(scores, default=0))Expected outputNone
0
Common mistakes
Calling min() on an empty list
✗ Wrong
print(min([]))✓ Right
print(min([], default=0))Check the list first or pass default=.
Mixing numbers and strings
✗ Wrong
print(min(3, "2"))✓ Right
print(min(3, int("2")))Convert everything to the same type first.
Comparing numeric strings
✗ Wrong
print(min(["10", "9", "100"]))✓ Right
print(min(["10", "9", "100"], key=int))Convert to numbers or use key=int.
Python min() Function: questions people ask
How do I find the smallest number in a list in Python?
Use min(my_list), for example min([4, 1, 7]) returns 1. To get its position, use my_list.index(min(my_list)).
What does min() return when there is a tie?
It returns the first of the equally small items in iteration order.
How does min() work on a dictionary?
It compares the keys. To find the key with the smallest value, use min(d, key=d.get).
Keep learning
Step-by-step lessons:
Python ListsPython Sorting
Related:
Python max() FunctionPython sum() FunctionPython sorted() FunctionPython Program to Find the Largest Number in a List
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →