← Go Back
But a
How to Find the Smallest or Largest Number in a List
Themin()
and max()
built-in functions take any iterable object and return minimum or maximum element respectively. By default, they work with numbers.>>> min([3, 4, 1, 2])
1
>>> max([3, 4, 1, 2])
4
But a
key
function can be passed to determine the criterion according to which one element is greater than another. For example:def population(country):
return {
"United States": 327,
"China": 1391,
"Indonesia": 264,
"India": 1364
}[country]
countries = ["United States", "China", "Indonesia", "India"]
print(max(countries, key=population)) # China.
print(min(countries, key=population)) # Indonesia.
🐍 You might also find interesting: