ML Foundations

Python mode and multimode when values tie

Use statistics.mode for one most-common value and statistics.multimode when every tied mode matters.

Example results for Python mode and multimode when values tie. single mode: blue
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

A tie does not make mode fail in current Python

When several values share the highest count, `statistics.mode` still returns one value. Since Python 3.8, the function resolves a tie by returning the first mode encountered in the input. Older Python versions raised `StatisticsError` for a multimodal dataset, so version context matters when reproducing old behavior. [1, 2]

The selected value is determined by encounter order among the tied modes, not by sorting values into numeric or alphabetical order. If a stable application rule matters, make that rule explicit in the calling code instead of treating the returned first mode as an inherent property of the data. [1]

References: [1], [2]

Use multimode to keep every tied value

`statistics.multimode` returns all values with the greatest frequency, in the order those values first appeared. The example gives blue and red equal counts: `mode` returns blue, while `multimode` returns both. The assertions make both contracts visible for a nominal string dataset. [1]

This distinction is useful when ties are meaningful, such as reporting every most-common category. If a single category is required, choose the tie-breaking rule deliberately; taking the first element from `multimode` follows encounter order, while `min` or `max` supplies an explicit ordering rule for comparable values. [1]

References: [1]

Choose a result shape that fits the question

Use `mode` when the consumer needs one representative most-common value and first-seen tie handling is acceptable. Use `multimode` when returning all tied categories is the correct answer. Both functions work with discrete or nominal data, and an empty input is a separate case: `mode` raises `StatisticsError`, whereas `multimode` returns an empty list. [1]

This narrow example checks only a nonempty list of hashable strings. It does not cover NaN values, unhashable records, numeric sorting rules, or every historical Python version. The example ran under Python 3.12.14, where multimodal `mode` returns the first encountered tied value. [1, 2]

Executed with Python 3.12.14; assertions passed.

from statistics import mode, multimode

values = ["blue", "red", "blue", "red"]
first_mode = mode(values)
all_modes = multimode(values)

print("single mode:", first_mode)
print("all modes:", all_modes)

assert first_mode == "blue"
assert all_modes == ["blue", "red"]

Recorded output

single mode: blue
all modes: ['blue', 'red']

References: [1], [2]

Sources

  1. Python statistics mode and multimode
  2. Python 3.8 statistics change