What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More
Computer Science

Python min() and max() Functions with Examples

Jul 19, 2026 4 Minutes Read Why Trust Us Why you can trust this guide. Written by working engineers and reviewed by our editorial team under a strict editorial policy for accuracy, clarity and zero bias. Komal Gupta By Komal Gupta Komal Gupta Komal Gupta
I am a driven and ambitious individual who is passionate about programming and technology. I have experience in Java, Python, and machine learning, and I am constantly seeking to improve and expand my knowledge in these areas. I am an AI/ML researcher. I enjoy sharing my technical knowledge as a content writer to help the community.
Connect on LinkedIn →
Python min() and max() Functions with Examples

In Python, min() and max() are built-in functions that return the smallest and largest value from a list, another iterable, or a set of separate arguments. They work on numbers, strings, and any values that can be compared with each other.

Both functions take the same arguments: an iterable or several values, an optional key function that changes how items are compared, and an optional default for empty iterables. This lesson shows how to use each form.

What Do the min() and max() Functions Do in Python?

min() returns the smallest value and max() returns the largest value from the values you pass in.

# 1. A list of exam scores
scores = [67, 91, 54, 88]

# 2. Smallest and largest
print(min(scores))  # Outputs: 54
print(max(scores))  # Outputs: 91
min() and max() scanning a Python list of scores once and returning 54 and 91

How to Use the Max Function in Python

1) Max of a List

Pass a list (or any iterable) as a single argument and max() returns its largest element.

daily_sales = [1240, 980, 1675, 1420]

print(max(daily_sales))  # Outputs: 1675

2) Max of Several Arguments

Pass two or more separate values and max() compares them directly. This form is common for clamping a number to a lower limit.

print(max(37, 12, 58))  # Outputs: 58

balance = -45
print(max(balance, 0))  # Outputs: 0

3) Max with the key Argument

The key argument takes a function, and max() compares the items by that function's result instead of the items themselves. The return value is the original item, not the key result.

usernames = ["kai", "alexandra", "meera"]

print(max(usernames, key=len))  # Outputs: alexandra

4) Max of Strings

With strings, max() compares alphabetically by Unicode code point, so "zoe" is greater than "adam". Uppercase letters sort before lowercase ones.

names = ["adam", "zoe", "lena"]

print(max(names))  # Outputs: zoe

How to Use the Min Function in Python

The min() function accepts the same forms as max(): one iterable, several arguments, and an optional key.

ticket_prices = [49.99, 32.50, 75.00]

print(min(ticket_prices))          # Outputs: 32.5
print(min(18, 5, 92))              # Outputs: 5
print(min(["oslo", "rio", "ulm"], key=len))  # Outputs: rio

When two items tie under key, both functions return the first one they meet, so min(["rio", "ulm"], key=len) returns "rio".

Min and Max of an Empty List

Calling either function on an empty iterable raises a ValueError.

refunds = []

print(max(refunds))
# ValueError: max() arg is an empty sequence

The default argument returns a fallback value instead of raising the error. It only works with the single-iterable form.

refunds = []

print(max(refunds, default=0))  # Outputs: 0

When to Use min() and max() in Python

1) Finding an Extreme in Data

Use them to pull the highest score, the cheapest price, or the longest name out of a collection in one call. Both functions scan the values once, so they run in O(n) time.

2) Clamping a Number to a Range

Combining the two keeps a value inside fixed limits: max(low, min(value, high)).

volume = 130

clamped = max(0, min(volume, 100))
print(clamped)  # Outputs: 100
A value of 130 clamped down to the allowed range of 0 to 100 with max() and min() in Python

3) Picking a Record by One Field

With key, they select a whole record based on a single field, such as the product with the highest price.

Examples of Using min() and max() in Python

1) Highest Value in a Dictionary

On a dictionary, the functions compare the keys. Pass dict.get as the key argument to compare by value instead.

page_views = {"home": 5120, "pricing": 1890, "docs": 7301}

print(max(page_views))                     # Outputs: pricing
print(max(page_views, key=page_views.get)) # Outputs: docs

The first call returns "pricing" because it is alphabetically the largest key.

2) Cheapest Product in a List of Tuples

products = [("keyboard", 89.99), ("mouse", 24.50), ("monitor", 219.00)]

cheapest = min(products, key=lambda item: item[1])
print(cheapest)  # Outputs: ('mouse', 24.5)

3) Temperature Range of a Week

temps = [18.4, 22.1, 16.9, 24.3, 20.0]

print(max(temps) - min(temps))  # Outputs: 7.400000000000002
print(round(max(temps) - min(temps), 1))  # Outputs: 7.4

The long decimal comes from floating-point arithmetic, so round() is used for display.

Learn More About min() and max()

The Index of the Min or Max Value

The functions return the value, not its position. Combine them with index() to get the position of the extreme.

laps = [72.4, 69.8, 71.1]

fastest = min(laps)
print(laps.index(fastest))  # Outputs: 1

Min and Max of Mixed Types

Values must be comparable with each other. Comparing a number with a string raises a TypeError.

print(max([3, "ten", 7]))
# TypeError: '>' not supported between instances of 'str' and 'int'

NumPy min and max for Arrays

For large numeric arrays, NumPy's np.min() and np.max() run the scan in compiled code, which is faster than the built-ins on plain lists.

import numpy as np

readings = np.array([412, 388, 405, 391])
print(np.min(readings))  # Outputs: 388
print(np.max(readings))  # Outputs: 412

Key Takeaways for min() and max() in Python

  • Two forms - pass one iterable or several separate values; both functions accept the same arguments.
  • key - compares items by a function's result but returns the original item.
  • default - returns a fallback for an empty iterable instead of raising ValueError.
  • Strings - compared alphabetically by Unicode code point, so case affects the order.
  • Cost - one scan through the values, O(n) time, no sorting involved.

A common companion task is finding the most frequent value rather than the largest one. For that, read our lesson on the Python Counter and its most_common() method.

Komal Gupta
About the author

Komal Gupta

I am a driven and ambitious individual who is passionate about programming and technology. I have experience in Java, Python, and machine learning, and I am constantly seeking to improve and expand my knowledge in these areas. I am an AI/ML researcher. I enjoy sharing my technical knowledge as a content writer to help the community. Connect on LinkedIn →