• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Calculate Standard Deviation of List of Numbers in Python

Calculate Standard Deviation of List of Numbers in Python

August 26, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Python Prepend to List with insert() Function
  • 2.  How Clear a Set and Remove All Items in Python
  • 3.  Using Python to Check If a Number is a Perfect Square
  • 4.  Using Python to Remove First Character from String
  • 5.  Get pandas Index Values as List in Python
  • 6.  Python power function – Exponentiate Numbers with math.pow()
  • 7.  How to Group By Columns and Find Standard Deviation in pandas
  • 8.  Initialize Multiple Variables in Python
  • 9.  Print Multiple Variables in Python
  • 10.  Get Days in Month from Date in pandas DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy