Table of Contents
The standard deviation is a statistical measure that indicates the spread or variability of a set of data from its mean. In Python, the standard deviation of a list can be calculated using the “statistics” module. This module provides a function called “stdev()” which takes in a list of numerical values as an argument and returns the standard deviation of the list. This function uses the formula for standard deviation, which involves finding the mean, calculating the difference between each data point and the mean, squaring the differences, taking the sum, dividing by the number of data points, and finally taking the square root. By using the “stdev()” function, one can easily and accurately calculate the standard deviation of a list in Python.
Calculate the Standard Deviation of a List in Python
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 listnp.std(my_list)
Method 2: Use statistics Library
import statistics as stat #calculate standard deviation of liststat.stdev(my_list)
Method 3: Use Custom Formula
#calculate standard deviation of listst.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 listnp.std(my_list, ddof=1) 5.310367218940701 #calculate population standard deviation of listnp.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 liststat.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)))**2for 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)))**2for 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.
Cite this article
stats writer (2024). How can I calculate the standard deviation of a list in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-the-standard-deviation-of-a-list-in-python/
stats writer. "How can I calculate the standard deviation of a list in Python?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-the-standard-deviation-of-a-list-in-python/.
stats writer. "How can I calculate the standard deviation of a list in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-the-standard-deviation-of-a-list-in-python/.
stats writer (2024) 'How can I calculate the standard deviation of a list in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-the-standard-deviation-of-a-list-in-python/.
[1] stats writer, "How can I calculate the standard deviation of a list in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I calculate the standard deviation of a list in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
