Python Glossary Русский

Python 3.4+statisticsfunction

statistics.mode

mode

Mode — the most common value (the first one on a tie).

Syntax

statistics.mode(data)

Since Python 3.8 mode no longer raises StatisticsError when several values tie for most frequent — it quietly returns the first one in order of appearance, so a tie passes unnoticed; reach for multimode when the tie matters. Empty data still raises StatisticsError, and elements must be hashable because counting goes through Counter.

Examples

import statistics
print(statistics.mode([1, 1, 2, 3]))       # → 1
print(statistics.mode(['a', 'b', 'a']))   # → a
print(statistics.mode('banana'))           # → a
print(statistics.mode([1, 1, 2, 2]))       # → 1
print(statistics.multimode([1, 1, 2, 2]))  # → [1, 2]
print(statistics.mode([]))                 # → StatisticsError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy