Python min() function returns the smallest element in a collection of items (list, tuple, set, string, or other iterables) or among multiple arguments.
numbers_list = [7, 1, 3, 5] print('The smallest number from numbers_list is:', min(numbers_list)) # Output: The smallest number from numbers_list is: 1
As you can see, it returns the lowest value from the iterable.
It compares items using the < operator by default, but can be customized with a key function.
If the iterable is empty and no default value is provided, it raises a ValueError.
Syntax
min(iterable, key, default)
Parameters
Argument | Description |
iterable | It represents an iterable, one or more elements to compare. It iterates over this to find the minimum. |
key (optional) |
It represents a callable function that accepts a single argument and returns a value used for comparison. |
default (optional, default not set; iterable form only) |
It is a default value that is returned if the iterable is empty. If omitted and the iterable is empty, it will throw ValueError. |
With a Key Function
We can use a key function to set the criteria for finding the minimum value.
data = [{"name": "Jon", "age": 28}, {"name": "Mark", "age": 25}] youngest_person = min(data, key=lambda x: x['age']) print(youngest_person) # Output: {'name': 'Mark', 'age': 25}
In this code, we find the minimum value based on a person’s age. Since Mark is 25 years old, we get a dictionary with the lowest age.
Individual elements
As we know, we can pass multiple arguments to the min() function, and it will return the minimum element from them.
smallest_number = min(8, 4, 17, 5) print("The smallest number is:", smallest_number) # Output: The smallest number is: 4
With Strings (Lexicographical Order)
The min() function returns the string that is smallest in lexicographical (i.e., alphabetical) order.
It compares strings alphabetically (case-sensitive; uppercase letters appear before lowercase letters).
smallest_string = min("Ford", "Tesla", "Audi") print("The smallest string is:",smallest_string) # Output: The smallest string is: Audi
In the first step, it compares “Ford” vs “Tesla” and the First letters: “F” (Unicode 70) vs “T” (Unicode 84). Since 70 < 84, “Ford” is smaller than “Tesla”.
In step two, it compares “Ford” vs “Audi” in which the first letters: “F” (70) vs “A” (65). Since 65 < 70, “Audi” is smaller.
Handling empty iterables
If you pass an empty iterable to min(), it will raise a ValueError.
print(min([])) # Output: ValueError: min() arg is an empty sequence
To handle this, you can specify a default value using the default parameter.
print(min([], default="Empty")) # Output: Empty
With Tuples (Element-Wise Comparison)
When you use the min() function on a list of tuples, comparison happens lexicographically, just like how words are compared in a dictionary.
- Compare the first elements of the tuples.
- If the first elements are equal, compare the second elements.
- Continue element by element until a difference is found.
points = [(2, 3), (1, 4), (1, 2)] min_point = min(points) # Compares first elements, then second if tied print(min_point) # Output: (1, 2)
In the above code, it compares (2, 3) with (1, 4). Now, 2 vs 1 → since 1 < 2, (1, 4) is smaller.
In the next step, it compares (1, 4) with (1, 2). First elements are equal (1 == 1), so compare the second elements. Since 2 < 4, (1, 2) is smaller. That is why (1, 2) is the output.
With Dictionaries
When you call min() on a dictionary, Python doesn’t look at the values by default — it works on the keys.
grades = {"Ankit": 85, "Krunal": 72, "Rushabh": 90} print(min(grades)) # Output: "Ankit" (lexicographically smallest key) print(min(grades, key=grades.get)) # Output: "Krunal" (student with lowest grade)
The min() function finds the smallest key lexicographically (alphabetical order).
“Ankit” < “Krunal” < “Rushabh”, so “Ankit” is in the output.
When it comes to the key=grades.get, it tells Python to compare dictionary keys based on their values (the grades).
Behind the scenes:
- “Ankit” → 85
- “Krunal” → 72
- “Rushabh” → 90
The lowest grade is 72, which belongs to “Krunal”.
That’s all!