Codectionary / Developer documentation / Python

min() and max()

Select an extreme value or record without sorting the whole collection.

How it works

min and max return an input item, not its key. The key argument controls comparison. When several items tie, the first encountered item wins.

The iterable form accepts default for empty inputs. Without a default an empty iterable raises ValueError. The separate-positional-arguments form does not accept default.

Choices and common mistakes

Choose a default that callers can distinguish from a real result. None is often useful, but it still needs handling.

Finding one extreme takes a traversal and does not rearrange the data. Sorting is appropriate when you need a complete ranking.

Try it yourself

Find the cheapest available product, returning None for an empty inventory. Explain what should happen when two products have the same price.

Syntax

min(values, default=None)
max(records, key=lambda record: record["score"])

Examples

Measure a range

Get the smallest and largest recorded measurements.

readings = [18, 22, 19]
print(min(readings), max(readings))
print(readings)
  1. Each function inspects the values and selects an extreme. Neither mutates the list.
  2. The second line confirms that the original measurement order is preserved. Two function calls mean two traversals.
  3. Use this for small summary reports; maintain running extrema when processing a large one-shot stream.
  4. Expected output: 18 22 [18, 22, 19]

Select a whole record

Keep the information associated with the winning score.

players = [("John", 8), ("James", 8), ("Sam", 5)]
winner = max(players, key=lambda player: player[1])
print(winner)
  1. The lambda selects index one, the score, for comparison. max still returns the complete tuple.
  2. John and James tie, so the first encountered record wins. This is a selection rule, not a random tie-break.
  3. Selecting the record keeps its name available for a leaderboard summary.
  4. Expected output: ('John', 8)

Handle no readings

Avoid an exception when an empty input is expected.

readings = []
lowest = min(readings, default=None)
if lowest is None:
    print("No readings")
else:
    print(lowest)
  1. The empty iterable selects the explicit default. An identity check separates that sentinel from a numeric reading.
  2. A reading of zero would reach the else branch; a general truthiness check would incorrectly confuse it with absence.
  3. Use an explicit missing-result policy when sensor data or query results may be absent.
  4. Expected output: No readings

Best practices

  • Choose a default that callers can distinguish from a real result. None is often useful, but it still needs handling.
  • Finding one extreme takes a traversal and does not rearrange the data. Sorting is appropriate when you need a complete ranking.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading