← Go Back

How to Find the Smallest or Largest Number in a List

The min() 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])
>>> max([3, 4, 1, 2])

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

countries = ["United States", "China", "Indonesia", "India"]
print(max(countries, key=population)) # China.
print(min(countries, key=population)) # Indonesia.

lists built-ins

🐍 You might also find interesting: