Reference · Statistics

stats.mode

signature
stats.mode(list) -> any

What 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
3red3

Edge cases

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 data
this one fails
print(stats.mode(5))
output
line 1: `stats.mode` expects a list as argument 1, got int

Chaining

let votes = ["yes", "no", "yes", "abstain"]print(votes | stats.mode)print(([3, 3, 4] | stats.mode) + 1)
output
yes4

See also

stats.multimode, stats.median, count, group_by

Thread
7Useful? Like it, or add to its thread below.

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