# Python max and min with key: pick by a rule

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-max-min-key-full-en
`max(items, key=func)` returns the item with the largest `func(item)`, and `min` returns the smallest. When several items tie, the first one is returned.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-102-A, TE-102-B and TE-102-C. The expected non-zero exit is TE-102-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-102-A: max and min with key=len, and the first item wins a tie in max

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
words = ['pear', 'fig', 'plum', 'banana']
print(max(words, key=len))
print(min(words, key=len))
print(max(['aa', 'bb', 'c'], key=len))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
banana
fig
aa
```

Exit status: 0. The output matches the expected text written before the run.

### TE-102-B: max accepts default= for an empty list, and min returns the first of several tied items

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
print(max([], default='none'))
print(min(['bb', 'aa', 'c', 'd'], key=len))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
none
c
```

Exit status: 0. This status and the output match what was written before the run.

### TE-102-C: expected non-zero exit: max of an empty list without default= raises ValueError

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
max([])
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b24-ygu1tvvo/work/s.py", line 1, in <module>
    max([])
ValueError: max() arg is an empty sequence
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-102-B: max of an empty list with default='none' returned none. min with key=len returned c, the first of the two items with length 1. TE-102-C: max([]) stopped with exit status 1. The last line of standard error is ValueError: max() arg is an empty sequence.

## Limits

The docs state the tie rule for max and min. TE-102-A checks it for max and TE-102-B for min. TE-102-C runs an empty list without `default=`, and TE-102-B runs `default=`. Not tested on this page: the form with two or more positional arguments, such as comparing several plain values instead of one iterable. The help text of Python 3.10.12 lists that form of `max` and `min` next to the single-iterable form, and says it returns the largest or the smallest argument. No run here used it.

## Source

- https://docs.python.org/3/library/functions.html#max

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-102-A` | max and min with key=len, and the first item wins a tie in max | 0 | PASS | `cebb1d5d73cd` |
| `TE-102-B` | max accepts default= for an empty list, and min returns the first of several tied items | 0 | PASS | `3c774e149c0c` |
| `TE-102-C` | expected non-zero exit: max of an empty list without default= raises ValueError | 1 | PASS | `f1204c1b214a` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python itertools.chain: loop over several sequences as one](https://kairos-2z6s.onrender.com/res/python-itertools-chain-full-en)
- [Python try and finally: code that always runs](https://kairos-2z6s.onrender.com/res/python-try-finally-full-en)
- [Python modules and imports](https://kairos-2z6s.onrender.com/res/python-modules-imports-full-en)
- [Python lru_cache: remember function results](https://kairos-2z6s.onrender.com/res/python-lru-cache-full-en)
- [Python Loop Else: runs when the loop did not break](https://kairos-2z6s.onrender.com/res/python-loop-else-full-en)
