HOWTO · Python

How to Calculate the Standard Deviation of a List in Python

Calculate population standard deviation in Python with statistics, NumPy, or the formula, and learn when to use a sample calculation.

On this page

Use statistics.pstdev() to calculate the population standard deviation of a Python list. NumPy’s numpy.std() also calculates the population value by default, while a short formula shows how the calculation works. If your data is a sample used to estimate a larger population, use a sample standard deviation instead.

Standard deviation describes how far values typically vary from their mean. The examples below use the same list so you can compare the built-in, NumPy, and manual approaches directly. (statistics.stdev())

Calculate population standard deviation with statistics.pstdev()

Python’s standard-library statistics module provides statistics.pstdev() for population standard deviation. It calculates the mean from the values unless you supply the optional mu argument yourself. For ordinary use, pass only the data so the function uses the matching mean.

Run this example:

import statistics

data = [2, 4, 4, 4, 5, 5, 7, 9]
standard_deviation = statistics.pstdev(data)
print(f"Population standard deviation: {standard_deviation}")

This is the simplest option when the list contains the complete population you want to describe. The result is a floating-point number; keep it numeric for later calculations, and format it only when displaying it.

Calculate population standard deviation with NumPy

For array-based work, use numpy.std(). Its default ddof=0 divides the sum of squared differences by the number of values, which gives population standard deviation. NumPy must be installed in the Python environment before this example can run.

Run this example:

import numpy as np

data = [2, 4, 4, 4, 5, 5, 7, 9]
standard_deviation = np.std(data)
print(f"Population standard deviation: {standard_deviation}")

When the list is a sample and you want the sample standard deviation, set ddof=1: np.std(data, ddof=1). This changes the divisor from the number of values to one less than that count. For multidimensional arrays, axis selects the dimension along which NumPy computes the result; with a one-dimensional list, the default computes one value for the whole list.

Calculate population standard deviation manually

The population formula has three steps: find the mean, average the squared difference between each value and that mean, then take the square root of that average. Dividing by the number of values is what makes this the population calculation. math.sqrt() is one clear way to take the final square root.

Run this example:

import math

data = [2, 4, 4, 4, 5, 5, 7, 9]
mean = sum(data) / len(data)
squared_differences = [(value - mean) ** 2 for value in data]
population_variance = sum(squared_differences) / len(data)
standard_deviation = math.sqrt(population_variance)
print(f"Population standard deviation: {standard_deviation}")

This version makes each part of the formula visible and uses only the standard library. The same final square root can also be written as population_variance ** 0.5. The input must contain numeric values and at least one item: the mean step divides by the list length, so decide how your application should handle a list with no values before calculating.

Choose population or sample standard deviation

Use population standard deviation when your data includes every member of the group you are describing, such as all measurements collected for a defined batch. Use sample standard deviation when your values are only a sample and you use them to estimate variation in a larger population. The sample formula applies a degrees-of-freedom correction because the sample mean was estimated from those same observations.

In Python, statistics.pstdev(data) calculates the population value and statistics.stdev(data) calculates the sample value. NumPy uses np.std(data) for the population by default and np.std(data, ddof=1) for the sample form. These choices can produce different results for the same list, so choose based on what the data represents rather than on which function is shorter.