Summary: in this tutorial, you’ll learn how to use the Python min()
function to return the smallest item of an iterable or the smallest argument of two or more arguments.
Introduction to the Python min() function
The min()
function returns the smallest items of two or more arguments:
min(arg1,arg2,*args[,key])
Code language: Python (python)
In this syntax:
arg1
,arg2
, .. are the input arguments.key
is an optional keyword-only parameter that specifies a key for comparing the arguments.
The min()
function also accepts an iterable and returns the smallest item of the iterable:
min(iterable, *[, key, default])
Code language: Python (python)
In this syntax:
iterable
is the input iterable.key
is an optional keyword-only argument that specifies the key for comparing the items of the iterable.default
is an optional keyword-only argument that specifies a value that themin()
function returns if the iterable is empty. If theiterable
is empty and thedefault
argument is omitted, themin()
function raises aValueError
exception.
If multiple items are minimal, the min()
function returns the first one it encounters.
Python min() function examples
Let’s take some examples of using the Python min()
function.
1) Using the Python min() function to find the minimum argument between multiple arguments
The following example uses the min()
function to get the smallest numbers:
result = min(1, 2, 3)
print(result) # 1
Code language: Python (python)
If you pass multiple strings, the min()
function returns the minimum string based on alphabetical order:
result = min('John', 'Alice', 'Jane')
print(result) # Alice
Code language: Python (python)
The following example shows how to use the min()
function with user-defined objects:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f'Name:{self.name}, age:{self.age}'
result = min(
Person('John', 21),
Person('Alice', 22),
Person('Jane', 20),
key=lambda p: p.age
)
print(result)
Code language: Python (python)
Output:
Name:Jane, age:20
Code language: Python (python)
How it works.
First, define the Person
class with two attributes, name
and age
:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f'Name:{self.name}, age:{self.age}'
Code language: Python (python)
Second, use the min()
function to find the person with the smallest age:
result = min(
Person('John', 21),
Person('Alice', 22),
Person('Jane', 20),
key=lambda p: p.age
)
Code language: Python (python)
Besides passing the Person
objects, we also specify the age
as the key to comparing these Person
objects.
2) Using the Python min() function to find the minimum item of an iterable
The following example uses the min()
function to find the smallest number of a list:
scores = [1, 5, 3, 2]
min_score = min(scores)
print(min_score) # 1
Code language: Python (python)
Similarly, this example uses the min()
function to find the minimum name
based on alphabetical order:
names = ['John', 'Alice', 'Jane']
name = min(names)
print(name) # Alice
Code language: Python (python)
This example illustrates how to use the min()
function to find the youngest person:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return self.name
people = (
Person('John', 21),
Person('Alice', 22),
Person('Jane', 20)
)
youngest_person = min(people, key=lambda p: p.age)
print(youngest_person)
Code language: Python (python)
Output:
Name:Jane, age:20
Code language: Python (python)
In this example, we pass a list of Person
objects and use the min()
function to find the youngest person who has the smallest age.
Summary
- Use the
min()
function to find the minimum item of arguments or of items in an iterable. - Use the
key
parameter to specify the key to compare the objects.