22.9 C
London
Monday, July 21, 2025
HomePythonDescriptive Statistics in PythonHow to Calculate the Standard Deviation of a List in Python

How to Calculate the Standard Deviation of a List in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use one of the following three methods to calculate the standard deviation of a list in Python:

Method 1: Use NumPy Library

import numpy as np

#calculate standard deviation of list
np.std(my_list)

Method 2: Use statistics Library

import statistics as stat

#calculate standard deviation of list
stat.stdev(my_list)

Method 3: Use Custom Formula

#calculate standard deviation of list
st.stdev(my_list)

The following examples show how to use each of these methods in practice.

Method 1: Calculate Standard Deviation Using NumPy Library

The following code shows how to calculate both the sample standard deviation and population standard deviation of a list using NumPy:

import numpy as np

#define list
my_list = [3, 5, 5, 6, 7, 8, 13, 14, 14, 17, 18]

#calculate sample standard deviation of list
np.std(my_list, ddof=1)

5.310367218940701

#calculate population standard deviation of list 
np.std(my_list)

5.063236478416116

Note that the population standard deviation will always be smaller than the sample standard deviation for a given dataset.

Method 2: Calculate Standard Deviation Using statistics Library

The following code shows how to calculate both the sample standard deviation and population standard deviation of a list using the Python statistics library:

import statistics as stat

#define list
my_list = [3, 5, 5, 6, 7, 8, 13, 14, 14, 17, 18]

#calculate sample standard deviation of list
stat.stdev(my_list)

5.310367218940701

#calculate population standard deviation of list 
stat.pstdev(my_list)

5.063236478416116

Method 3: Calculate Standard Deviation Using Custom Formula

The following code shows how to calculate both the sample standard deviation and population standard deviation of a list without importing any Python libraries:

#define list
my_list = [3, 5, 5, 6, 7, 8, 13, 14, 14, 17, 18]

#calculate sample standard deviation of list
(sum((x-(sum(my_list) / len(my_list)))**2 for x in my_list) / (len(my_list)-1))**0.5

5.310367218940701

#calculate population standard deviation of list 
(sum((x-(sum(my_list) / len(my_list)))**2 for x in my_list) / len(my_list))**0.5

5.063236478416116

Notice that all three methods calculated the same values for the standard deviation of the list.

Additional Resources

How to Calculate the Standard Error of the Mean in Python
How to Calculate Mean Squared Error (MSE) in Python

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories