Reference · Statistics

stats.median

signature
stats.median(list) -> num

What it does

The middle value of a list of numbers, as Python's statistics.median. The list need not be sorted. With an odd count it is the middle data point, and an int stays an int. With an even count it is the mean of the two middle values, a float.

Examples

print(stats.median([1, 3, 2]), stats.median([1, 2, 3, 4]))print(stats.median([1, 2, 4, 4]), stats.median([1.0, 3.0, 2.0]))
output
2 2.53.0 2.0

Edge cases

print(stats.median([3]), stats.median([2, 2]), stats.median([1, 2.0, 3]))print(stats.median([5, 5, 5, 1, 9]))
output
3 2.0 2.05

Errors

The median of no data is a runtime error, not an error value: an empty list or null stops the request. So does an item that is not a number.

this one fails
print(stats.median([]))
output
line 1: `stats.median` requires at least one data point
this one fails
print(stats.median(["a", "b"]))
output
line 1: `stats.median` expects a list of numbers, but item 0 is str

Chaining

print([9, 1, 5, 3] | stats.median)print(([9, 1, 5, 3] | stats.median) == stats.percentile([9, 1, 5, 3], 50))
output
4.0true

See also

stats.median_low, stats.median_high, stats.mean, stats.percentile, stats.quantiles

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

Thread 0 posts

On stats.median(): 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