Calculate the Standard Deviation of a List in Python

Use the
pstdev()
Function of thestatistics
Module to Calculate the Standard Deviation of a List in Python 
Use the
std()
Function of theNumPy
Library to Calculate the Standard Deviation of a List in Python 
Use the
sum()
Function and List Comprehension to Calculate the Standard Deviation of a List in Python
In Python, there are a lot of statistical operations being carried out. One of these operations is calculating the standard deviation of a given data. The standard deviation of data tells us how much the data has deviated from the mean value. Mathematically, the standard deviation is equal to the square root of variance.
This tutorial will demonstrate how to calculate the standard deviation of a list in Python.
Use the pstdev()
Function of the statistics
Module to Calculate the Standard Deviation of a List in Python
The pstdev()
function is one of the commands under Python’s statistics
module. The statistics
module provides functions to perform statistical operations like mean, median, and standard deviation on numeric data in Python.
The pstdev()
function of the statistics
module helps a user to calculate the standard deviation of the whole population.
import statistics
list = [12, 24, 36, 48, 60]
print("List : " + str(list))
st_dev = statistics.pstdev(list)
print("Standard deviation of the given list: " + str(st_dev))
Output:
List : [12, 24, 36, 48, 60]
Standard deviation of the given list: 16.97056274847714
In the above example, the str()
function converts the whole list and its standard deviation into a string because it can only be concatenated with a string.
Use the std()
Function of the NumPy
Library to Calculate the Standard Deviation of a List in Python
The NumPy
stands for Numerical Python
is a widely used library in Python. This library helps in dealing with arrays, matrices, linear algebra, and Fourier transform.
The std()
function of the NumPy
library is used to calculate the standard deviation of the elements in a given array(list). Check the example below.
import numpy as np
list = [12, 24, 36, 48, 60]
print("List : " + str(list))
st_dev = np.std(list)
print("Standard deviation of the given list: " + str(st_dev))
Output:
List : [12, 24, 36, 48, 60]
Standard deviation of the given list: 16.97056274847714
Use the sum()
Function and List Comprehension to Calculate the Standard Deviation of a List in Python
As the name suggests, the sum()
function provides the sum of all the elements of an iterable, like lists or tuples. The list comprehension is a method of creating a list from the elements present in an already existing list.
The sum()
function and list comprehension can help calculate the standard deviation of a list. Here’s an example code.
import math
list= [12, 24, 36, 48, 60]
print("List : " + str(list))
mean = sum(list) / len(list)
var = sum((lmean)**2 for l in list) / len(list)
st_dev = math.sqrt(var)
print("Standard deviation of the given list: " + str(st_dev))
Output:
List : [12, 24, 36, 48, 60]
Standard deviation of the given list: 16.97056274847714
In the example above, the math
module is imported. It provides the sqrt()
function to calculate the square root of a given value. Also, note that the function len()
is also used. This function helps provide the length of the given list, for example, the number of elements in the list.
This method is based on the mathematical formula of standard deviation. First, we calculate the variance and then get its square root to find the standard deviation.
Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their realworld applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.
LinkedInRelated Article  Python Statistics
Related Article  Python List
 Convert a Dictionary to a List in Python
 Remove All the Occurrences of an Element From a List in Python
 Remove Duplicates From List in Python
 Get the Average of a List in Python
 What Is the Difference Between List Methods Append and Extend
 Convert a List to String in Python