min()
Returns the smallest item of a list or of several arguments, with a key function to decide what smallest means and a default for empty data.
min(iterable, *, key=None)
min(iterable, *, default, key=None)
min(arg1, arg2, *args, key=None)min() finds the smallest value. Give it a collection, min([7, 2, 9]), or the values directly, min(7, 2, 9): both give 2.
The key argument changes what is compared. min(words, key=len) finds the shortest word; min(prices, key=prices.get) finds the cheapest item in a dictionary. When several items tie, the first one wins.
Strings are compared letter by letter by their character codes, and every capital letter comes before every small letter. So min(["pear", "Zucchini"]) is "Zucchini". Use key=str.lower to compare without case. And an empty list raises ValueError unless you give a default.
Parameters
| Parameter | What it is |
|---|---|
iterable or arg1, arg2, …required | One collection to look through, or two or more separate values. |
keyoptional, default None | A function applied to each item; the items are compared by its results. The item itself is returned, not the key. |
defaultoptional | What to return when the iterable is empty. Only for the one-collection form. |
Return value
The smallest item itself (or default for an empty iterable).
Examples
1Smallest number
print(min(7, 2, 9))
print(min([3.5, -1, 0]))
print(min(range(10, 20)))2 -1 10
2Shortest word and cheapest product
words = ["banana", "fig", "cherry", "kiwi"]
print(min(words, key=len))
prices = {"tea": 2.5, "coffee": 3.2, "water": 1.0}
print(min(prices, key=prices.get))
print(min(prices.values()))fig water 1.0
min() over a dictionary looks at its keys. key=prices.get compares their prices but still returns the key.
3Lowest score with the student's name
results = [("Lena", 81), ("Omar", 67), ("Yuki", 93)]
name, score = min(results, key=lambda r: r[1])
print(f"Lowest: {name} with {score}")Lowest: Omar with 67
4Capital letters come first
fruits = ["pear", "apple", "Zucchini"]
print(min(fruits))
print(min(fruits, key=str.lower))Zucchini apple
5Empty data and a default
todays_orders = []
print(min(todays_orders, default="no orders"))
try:
min(todays_orders)
except ValueError:
print("ValueError: min() of an empty list")no orders ValueError: min() of an empty list
Common mistakes
Comparing numbers stored as text
Strings are compared character by character, so "10" is smaller than "9" (because "1" comes before "9"). Convert them, or use key=int.
sizes = ["9", "10", "12"]
print(min(sizes))
print(min(sizes, key=int))10 9
Mixing numbers and text
Python cannot say whether 3 is smaller than "two", so it raises TypeError. Clean the data first.
Questions people ask
How do I find the minimum value in a list in Python?
min(my_list). Add default=None if the list may be empty.
How do I get the index of the smallest item?
my_list.index(min(my_list)), or in one pass: min(range(len(my_list)), key=my_list.__getitem__).
How do I find the key with the smallest value in a dictionary?
min(d, key=d.get).
How do I get the 3 smallest items?
sorted(items)[:3], or heapq.nsmallest(3, items) for big data.