multimode Returns Every Tied Mode

Get the most frequent values with statistics.multimode, which keeps every tied value in first-seen order instead of returning one arbitrary winner.

PythonData Integrity

Code

pythonsnippet
16 lines
from statistics import multimode, mean

votes = ["skip", "merge", "merge", "rebase", "skip"]

# 平票时 multimode 一次性返回全部众数,按首现顺序
print(multimode(votes))
print(multimode([1, 2, 3]))          # 全部只出现一次 -> 全返回
print(multimode([]))                 # 空输入 -> 空列表,不抛异常

# 单一众数场景:取列表第一个即可
winner = multimode(votes)[0]
print("winner:", winner)

# multimode 也能吃数字,配合 mean 描述分布重心
temps = [21, 19, 21, 23, 19, 21]
print(multimode(temps), round(mean(temps), 1))

Output

Captured from a sandboxed run of this snippet in the build pipeline.

['skip', 'merge']
[1, 2, 3]
[]
winner: skip
[21] 20.7

How it works

The file’s state is a handful of top-level bindings: votes, winner, temps.

Caveats

  • Nothing to install: the snippet stays inside the standard library.

Putting it to use

Output lines are prefixed (winner:), so each step’s result reads straight off the prefixed output.

Interactive Tools for This Topic

FAQ

Is a package install part of the setup?
No. Installation is unnecessary because only built-in APIs are used. Every import resolves inside the standard library — `statistics`. Its state lives in module-level bindings: `votes`, `winner`, `temps`.