Reference · Statistics
stats.mode
signature
stats.mode(list) -> anyWhat it does
The most common value in a list, as Python's statistics.mode. The values can be
anything, text and booleans too, not only numbers. When several values are equally
common, the one seen first wins; stats.multimode
gives all of them.
Examples
print(stats.mode([1, 2, 2, 3, 3, 3]))print(stats.mode(["red", "blue", "red"]))print(stats.mode([3, 1, 2]))output
3red3Edge cases
- A tie goes to the value that appears first:
[1, 1, 2, 2]gives1,[2, 2, 1, 1]gives2. - No value repeated means every value is a mode, so the first one is the answer.
1and1.0count as the same value; the one seen first is given back.- Text
"1"is not the number1. null, lists and maps count too.
print(stats.mode([1, 1, 2, 2]), stats.mode([2, 2, 1, 1]))print(stats.mode([1.0, 1, 2]), stats.mode([1, "1", "1"]))print(stats.mode([null, null, 1]), stats.mode([{ a: 1 }, { a: 1 }, { a: 2 }]))output
1 21.0 1null { a: 1 }Errors
The mode of no data is a runtime error, not an error value: an empty list or null
stops the request. An argument that is not a list is a runtime error too.
this one fails
print(stats.mode([]))output
line 1: `stats.mode`: no mode for empty datathis one fails
print(stats.mode(5))output
line 1: `stats.mode` expects a list as argument 1, got intChaining
let votes = ["yes", "no", "yes", "abstain"]print(votes | stats.mode)print(([3, 3, 4] | stats.mode) + 1)output
yes4See also
stats.multimode, stats.median, count, group_by
Thread
Thread 0 posts
On stats.mode(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo