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.
Code
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.7How 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
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Statistics CalculatorCompute count, sum, mean, median, minimum, maximum, range, variance, and standard deviation for a number list.Text StatisticsCount characters, words, lines, sentences, paragraphs, and estimated reading time entirely in the browser with no text sent to a server.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.