How to Get the Standard Deviation of a Python List?

This article shows you how to calculate the standard deviation of a given list of numerical values in Python.

Definition and Problem Formulation

The standard deviation is defined as the square root of the variance.

In case you’ve attended your last statistics course a few years ago, let’s quickly recap the definition of variance: variance is the average squared deviation of the list elements from the average value.

Definition Variance Math

Standard deviation is simply the square root of the variance.

So, how to calculate the standard deviation of a given list in Python?

Solution Overview

Here are three methods to accomplish this:

  1. Method 1: Import the NumPy library with import numpy as np and call np.std(list).
  2. Method 2: Import the statistics library with import statistics and call statistics.stdev(list) to obtain a slightly different result because it’s normalized with (n-1) rather than n for n list elements — this is called Bessel’s correction.
  3. Method 3: In vanilla Python without external dependency, calculate the average as avg = sum(list)/len(list) and then calculate the variance using the one-liner (sum((x-avg)**2 for x in lst) / len(lst))**0.5.

In addition to these three methods, we’ll also show you how to compute the standard deviation in a Pandas DataFrame in Method 4.

But before we do this, let’s examine the first three methods in one Python code snippet:

lst = [1, 0, 1, 2]

# 1. NumPy Standard Deviation
import numpy as np
std = np.std(lst)
print(std)
# 0.7071067811865476

# 2. Statistics Standard Deviation
import statistics
std = statistics.stdev(lst)
print(std)
# 0.816496580927726

# 3. W/O External Dependency
avg = sum(lst) / len(lst)
var = sum((x-avg)**2 for x in lst) / len(lst)
std = var**0.5
print(std)
# 0.7071067811865476

Let’s dive into each of those methods next.

Method 1: Standard Deviation in NumPy Library

import numpy as np

lst = [1, 0, 1, 2]
std = np.std(lst)

print(std)
# 0.7071067811865476

In the first example, you create the list and pass it as an argument to the np.std(lst) function of the NumPy library.

πŸ’‘ Note: Python’s package for data science computation NumPy also has great statistics functionality. Specifically, the NumPy library also supports computations on basic collection types, not only on NumPy arrays. You can calculate all basic statistics functions such as average, median, variance, and standard deviation on NumPy arrays.

If you need to improve your NumPy skills, check out our in-depth blog tutorial.

You can also calculate the standard deviation of a NumPy array instead of a list by using the same method:

Simply import the NumPy library and use the np.std(a) method to calculate the average value of NumPy array a.

Here’s the code:

import numpy as np

a = np.array([1, 2, 3])
print(np.std(a))
# 0.816496580927726

Method 2: Standard Deviation in Statistics Library

import statistics

lst = [1, 0, 1, 2]
std = statistics.stdev(lst)
print(std)
# 0.816496580927726

In the second example, you calculate the standard devaition as follows.

Import the statistics library and call the function statistics.stdev(lst) to calculate the standard deviation of a given list lst. The only difference to the NumPy standard deviation is that the Bessel’s correction is applied: the result is divided by (n-1) rather than n.

If you need more background on this, click this wiki link.

Standard deviation is defined as the deviation of the data values from the average (wiki). It’s used to measure the dispersion of a data set.

Method 3: Vanilla Python Standard Deviation

lst = [1, 0, 1, 2]
avg = sum(lst) / len(lst)
var = sum((x-avg)**2 for x in lst) / len(lst)
std = var**0.5

print(std)
# 0.7071067811865476

In the third example, you first calculate the average as sum(list)/len(list).

Then, you use a generator expression (see list comprehension) to dynamically generate a collection of individual squared differences, one per list element, by using the expression (x-avg)**2.

You sum them up and normalize the result by dividing through the number of list elements to obtain the variance.

Method 4: Standard Deviation in Python Pandas

Want to calculate the standard deviation of a column in your Pandas DataFrame?

You can do this by using the pd.std() function that calculates the standard deviation along all columns. You can then get the column you’re interested in after the computation.

import pandas as pd

# Create your Pandas DataFrame
d = {'username': ['Alice', 'Bob', 'Carl'],
     'age': [18, 22, 43],
     'income': [100000, 98000, 111000]}
df = pd.DataFrame(d)

print(df)

Your DataFrame looks like this:


usernameageincome
0Alice18100000
1Bob2298000
2Carl43111000

Here’s how you can calculate the standard deviation of all columns:

print(df.std())

The output is the standard deviation of all columns:

age         13.428825
income    7000.000000
dtype: float64

To get the variance of an individual column, access it using simple indexing:

print(df.std()['age'])
# 180.33333333333334

Related Questions

This is the absolute minimum you need to know about calculating basic statistics such as the standard deviation (and variance) in Python.

But there’s far more to it and studying the other ways and alternatives will actually make you a better coder.

So, let’s dive into some related questions and topics you may want to learn!

Python List Median

What’s the median of a Python list? Formally, the median is “the value separating the higher half from the lower half of a data sample” (wiki).

How to calculate the median of a Python list?

  • Sort the list of elements using the sorted() built-in function in Python.
  • Calculate the index of the middle element (see graphic) by dividing the length of the list by 2 using integer division.
  • Return the middle element.

Together, you can simply get the median by executing the expression median = sorted(income)[len(income)//2].

Here’s the concrete code example:

income = [80000, 90000, 100000, 88000]

average = sum(income) / len(income)
median = sorted(income)[len(income)//2]

print(average)
# 89500.0

print(median)
# 90000.0

Related tutorials:

Python List Mean

The mean value is exactly the same as the average value: sum up all values in your sequence and divide by the length of the sequence.

You can use either the calculation sum(list) / len(list) or you can import the statistics module and call mean(list).

Here are both examples:

lst = [1, 4, 2, 3]

# method 1
average = sum(lst) / len(lst)
print(average)
# 2.5

# method 2
import statistics
print(statistics.mean(lst))
# 2.5

Both methods are equivalent. The statistics module has some more interesting variations of the mean() method (source):

mean()Arithmetic mean (β€œaverage”) of data.
median()Median (middle value) of data.
median_low()Low median of data.
median_high()High median of data.
median_grouped()Median, or 50th percentile, of grouped data.
mode()Mode (most common value) of discrete data.

These are especially interesting if you have two median values and you want to decide which one to take.

Python List Min Max

There are Python built-in functions that calculate the minimum and maximum of a given list. The min(list) method calculates the minimum value and the max(list) method calculates the maximum value in a list.

Here’s an example of the minimum, maximum, and average computations on a Python list:

import statistics as s

lst = [1, 1, 2, 0]
average = sum(lst) / len(lst)
minimum = min(lst)
maximum = max(lst)

print(average)
# 1.0

print(minimum)
# 0

print(maximum)
# 2

Where to Go From Here

Summary: how to calculate the standard deviation of a given list in Python?

  1. Import the NumPy library with import numpy as np and use the np.std(list) function.
  2. Import the statistics library with import statistics and call statistics.stdev(list) to obtain a slightly different result because it’s normalized with (n-1) rather than n for n list elements – this is called Bessel’s correction.
  3. Without External Dependency: Calculate the average as sum(list)/len(list) and then calculate the variance in a list comprehension statement.

If you keep struggling with those basic Python commands and you feel stuck in your learning progress, I’ve got something for you: Python One-Liners (Amazon Link).

In the book, I’ll give you a thorough overview of critical computer science topics such as machine learning, regular expression, data science, NumPy, and Python basics—all in a single line of Python code!

Get the book from Amazon!

OFFICIAL BOOK DESCRIPTION: Python One-Liners will show readers how to perform useful tasks with one line of Python code. Following a brief Python refresher, the book covers essential advanced topics like slicing, list comprehension, broadcasting, lambda functions, algorithms, regular expressions, neural networks, logistic regression and more. Each of the 50 book sections introduces a problem to solve, walks the reader through the skills necessary to solve that problem, then provides a concise one-liner Python solution with a detailed explanation.