Reference · Statistics
stats.median
signature
stats.median(list) -> numWhat 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.0Edge cases
- One number gives that number.
- An even count gives a float even when the two middle values are the same:
[2, 2]gives2.0. - If any item is a float, the answer is a float, also with an odd count.
- Ties are fine; only the position matters.
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.05Errors
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 pointthis one fails
print(stats.median(["a", "b"]))output
line 1: `stats.median` expects a list of numbers, but item 0 is strChaining
print([9, 1, 5, 3] | stats.median)print(([9, 1, 5, 3] | stats.median) == stats.percentile([9, 1, 5, 3], 50))output
4.0trueSee also
stats.median_low, stats.median_high, stats.mean, stats.percentile, stats.quantiles
Thread
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