Every week I end up looking at short lists of numbers: how many times a screen loop played, how long a job took to run, how many entries came in from a form. I used to paste them into a spreadsheet just to get an average. Now I reach for the statistics module that ships with Python. It needs no install, the code reads like plain English, and it pushes me toward better questions than "what is the average?"
The examples below use made up sample numbers so the code is easy to follow.
Start with mean and median together
from statistics import mean, median daily_visits = [42, 38, 45, 40, 41, 39, 160] print(round(mean(daily_visits), 1)) print(median(daily_visits)) The mean is 57.9 and the median is 41. One unusual day, say a street fair, pulls the mean far above what a normal day looks like. The median barely notices.
My rule: when I want a "typical day," I use the median. When I need a total for the week, I use the sum. The mean sits in between, and on a short list it is the number most likely to mislead me. If you have a large list of floats and only need speed, fmean does the same job as mean but faster.
Mode for the most common value
from statistics import mode, multimode busiest_hours = [11, 14, 11, 17, 14, 11, 16] print(mode(busiest_hours)) print(multimode([1, 1, 2, 2, 3])) mode returns 11, the single most common value. When there is a tie, it returns whichever tied value it saw first, which can quietly hide information. multimode returns every value tied for first, here [1, 2], so I use it whenever ties are likely, such as hours of the day or days of the week.
Spread matters as much as the middle
Two weeks can share the same average and feel completely different.
from statistics import mean, stdev week_a = [40, 41, 39, 40, 40] week_b = [20, 60, 25, 55, 40] for week in (week_a, week_b): print(mean(week), round(stdev(week), 1)) Both weeks average 40. Week A has a standard deviation of 0.7 and Week B has 17.7. Week A is steady. Week B swings so much that its average describes almost none of the actual days.
Two details worth knowing. stdev is the sample standard deviation, which is what you want when your list is a slice of a longer history. pstdev is for when the list is the entire population. And stdev needs at least two values, otherwise it raises StatisticsError, so I check the length first in any script that runs on its own.
Quartiles for a normal range
Averages are bad at answering "is this run unusual?" Quartiles are better.
from statistics import quantiles job_seconds = [2.1, 2.3, 2.2, 2.4, 2.2, 2.5, 2.3, 9.8, 2.2, 2.4] q1, q2, q3 = quantiles(job_seconds, n=4) print(q1, q2, q3) The three cut points come out to roughly 2.2, 2.3 and 2.4 seconds. The middle half of the runs sits in that narrow band. From there I use the common 1.5 times interquartile range rule to flag anything far outside it:
spread = q3 - q1 limit = q3 + 1.5 * spread slow = [s for s in job_seconds if s > limit] print(slow) That prints [9.8]. In a real log, a run like that is worth investigating, usually a slow network call or a retry, rather than averaging into "how long does this job take." I flag outliers first and decide what to do with them second. I never delete them silently.
Correlation, carefully
Python 3.10 added correlation to the module, which is handy for a quick look at whether two things move together.
from statistics import correlation hours_open = [8, 9, 10, 8, 11, 12, 9] visits = [35, 40, 44, 36, 48, 52, 41] print(round(correlation(hours_open, visits), 2)) This prints a value very close to 1, meaning longer days and more visits rise together in this sample. It does not prove that staying open longer causes more visits. Busy seasons could drive both. With only seven data points, I treat a strong correlation as a reason to keep collecting data, not as a conclusion.
A short checklist
Before I trust a weekly summary, I run through these:
- Report the median for a typical value and the sum for totals.
- Always show spread (stdev or quartiles) next to the middle.
- Use multimode when ties are possible.
- Flag outliers with the quartile rule, then investigate before removing anything.
- Guard stdev and quantiles against lists that are too short.
- Treat correlation on a short list as a hint, never a verdict.
None of this needs pandas or a notebook. A few lines from the standard library turn a list of numbers into a summary I can actually make decisions with, and the habit of asking "what is the spread?" has caught more mistakes for me than any single average ever did.
Originally published at tonycantave.com by Tony Sterlin Cantave, a Connecticut software developer.