To get the standard deviation of a list in Python, you can create your own function which will apply the standard deviation formula.
lst = [0, 3, 6, 5, 3, 9, 6, 2, 1]
def standard_dev(l):
mean = sum(l) / len(l)
return (sum([((x - mean) ** 2) for x in l]) / len(l)) ** 0.5
print(standard_dev(lst))
#Output:
2.6851213274654606
One other way to get the standard deviation of a list of numbers in Python is with the statistics module pstdsv() function.
import statistics
lst = [0, 3, 6, 5, 3, 9, 6, 2, 1]
print(statistics.pstdev(lst))
#Output:
2.6851213274654606
When working with collections of data in Python, the ability to summarize the data easily is valuable.
One such case is if you want to get the standard deviation of a list of numbers.
To get the standard deviation of a list in Python, you can create your own function which will apply the standard deviation formula.
The standard deviation requires us first to calculate the average of a list and then find the sum of squared residuals for each item in our list to the mean.
Then, you divide this number by the length of the list and take the square root.
We can program this formula in Python with list comprehension and basic mathematical operations.
Below is a simple example showing you how to calculate the standard deviation of a list in Python.
lst = [0, 3, 6, 5, 3, 9, 6, 2, 1]
def standard_dev(l):
mean = sum(l) / len(l)
return (sum([((x - mean) ** 2) for x in l]) / len(l)) ** 0.5
print(standard_dev(lst))
#Output:
2.6851213274654606
Using statistics Module pstdev() Function to Get Average of List in Python
A useful module in Python is the statistics module. The statistics module has many great functions for performing different calculations.
You can get the standard deviation of a list of numbers in Python is with the statistics module pstdsv() function.
Below shows you how to use pstdev() to get the standard deviation of a list in Python.
import statistics
lst = [0, 3, 6, 5, 3, 9, 6, 2, 1]
print(statistics.pstdev(lst))
#Output:
2.6851213274654606
Hopefully this article has been useful for you to learn how to calculate the standard deviation of a list using Python.