# How to Get the Standard Deviation of a Python List?

This article shows you how to calculate the standard deviation of a given list of numerical inputs in Python.

The standard deviation is defined as the square root of the variance.

In case you’ve attended your last statistics course a few years ago, let’s quickly recap the definition of variance: it’s the average squared deviation of the list elements from the average value.

So, how to calculate the standard deviation of a given list in Python?

1. Import the NumPy library with `import numpy as np` and use the `np.std(list)` function.
2. Import the `statistics` library with `import statistics` and call `statistics.stdev(list)` to obtain a slightly different result because it’s normalized with (n-1) rather than n for n list elements – this is called Bessel’s correction.
3. Without External Dependency: Calculate the average as `sum(list)/len(list)` and then calculate the variance in a list comprehension statement.

Let’s have a look at both methods in Python code:

```lst = [1, 0, 1, 2]

# 1. NumPy Standard Deviation
import numpy as np
std = np.std(lst)
print(std)
# 0.7071067811865476

# 2. Statistics Standard Deviation
import statistics
std = statistics.stdev(lst)
print(std)
# 0.816496580927726

# 3. W/O External Dependency
avg = sum(lst) / len(lst)
var = sum((x-avg)**2 for x in lst) / len(lst)
std = var**0.5
print(std)
# 0.7071067811865476```

Puzzle: Try to modify the elements in the list so that the standard deviation of variants (1) and (3) is 1.0 instead of 0.7071067811865476 in our interactive shell:

1. In the first example, you create the list and pass it as an argument to the `np.std(lst)` function of the NumPy library. Interestingly, the NumPy library also supports computations on basic collection types, not only on NumPy arrays. If you need to improve your NumPy skills, check out our in-depth blog tutorial.

2. In the second example, you import the statistics library and call the function `stdev()`. The only difference to the NumPy standard deviation is that the Bessel’s correction is applied: the result is divided by (n-1) rather than n. If you need more background on this, click this wiki link.

3. In the third example, you first calculate the average as `sum(list)/len(list)`. Then, you use a generator expression (see list comprehension) to dynamically generate a collection of individual squared differences, one per list element, by using the expression `(x-avg)**2`. You sum them up and normalize by the number of list elements to obtain the variance.

This is the absolute minimum you need to know about calculating basic statistics such as the standard deviation (and variance) in Python. But there’s far more to it and studying the other ways and alternatives will actually make you a better coder. So, let’s dive into some related questions and topics you may want to learn!

## Standard Deviation in Python Pandas

Want to calculate the standard deviation of a column in your Pandas DataFrame?

You can do this by using the `pd.std()` function that calculates the standard deviation along all columns. You can then get the column you’re interested in after the computation.

```import pandas as pd

d = {'username': ['Alice', 'Bob', 'Carl'],
'age': [18, 22, 43],
'income': [100000, 98000, 111000]}
df = pd.DataFrame(d)

print(df)```

Here’s how you can calculate the standard deviation of all columns:

`print(df.std())`

The output is the standard deviation of all columns:

```age         13.428825
income    7000.000000
dtype: float64```

To get the variance of an individual column, access it using simple indexing:

```print(df.std()['age'])
# 180.33333333333334```

Together, the code looks as follows. Use the interactive shell to play with it!

## Standard Deviation in NumPy Library

Python’s package for data science computation NumPy also has great statistics functionality. You can calculate all basic statistics functions such as average, median, variance, and standard deviation on NumPy arrays. Simply import the NumPy library and use the `np.var(a)` method to calculate the average value of NumPy array `a`.

Here’s the code:

```import numpy as np

a = np.array([1, 2, 3])
print(np.std(a))
# 0.816496580927726
```

## Standard Deviation in Statistics Library

Standard deviation is defined as the deviation of the data values from the average (wiki). It’s used to measure the dispersion of a data set. You can calculate the standard deviation of the values in the list by using the statistics module:

```import statistics as s

lst = [1, 0, 4, 3]
print(s.stdev(lst))
# 1.8257418583505538```

An alternative is to use NumPy’s `np.std(lst)` method.

## Python List Median

What’s the median of a Python list? Formally, the median is “the value separating the higher half from the lower half of a data sample” (wiki).

How to calculate the median of a Python list?

• Sort the list of elements using the `sorted()` built-in function in Python.
• Calculate the index of the middle element (see graphic) by dividing the length of the list by 2 using integer division.
• Return the middle element.

Together, you can simply get the median by executing the expression `median = sorted(income)[len(income)//2]`.

Here’s the concrete code example:

```income = [80000, 90000, 100000, 88000]

average = sum(income) / len(income)
median = sorted(income)[len(income)//2]

print(average)
# 89500.0

print(median)
# 90000.0```

Related tutorials:

## Python List Mean

The mean value is exactly the same as the average value: sum up all values in your sequence and divide by the length of the sequence. You can use either the calculation `sum(list) / len(list)` or you can import the `statistics` module and call `mean(list)`.

Here are both examples:

```lst = [1, 4, 2, 3]

# method 1
average = sum(lst) / len(lst)
print(average)
# 2.5

# method 2
import statistics
print(statistics.mean(lst))
# 2.5```

Both methods are equivalent. The `statistics` module has some more interesting variations of the `mean()` method (source):

These are especially interesting if you have two median values and you want to decide which one to take.

## Python List Min Max

There are Python built-in functions that calculate the minimum and maximum of a given list. The `min(list)` method calculates the minimum value and the `max(list)` method calculates the maximum value in a list.

Here’s an example of the minimum, maximum and average computations on a Python list:

```import statistics as s

lst = [1, 1, 2, 0]
average = sum(lst) / len(lst)
minimum = min(lst)
maximum = max(lst)

print(average)
# 1.0

print(minimum)
# 0

print(maximum)
# 2```

## Where to Go From Here

Summary: how to calculate the standard deviation of a given list in Python?

1. Import the NumPy library with `import numpy as np` and use the `np.std(list)` function.
2. Import the `statistics` library with `import statistics` and call `statistics.stdev(list)` to obtain a slightly different result because it’s normalized with (n-1) rather than n for n list elements – this is called Bessel’s correction.
3. Without External Dependency: Calculate the average as `sum(list)/len(list)` and then calculate the variance in a list comprehension statement.

If you keep struggling with those basic Python commands and you feel stuck in your learning progress, I’ve got something for you: Python One-Liners (Amazon Link).

In the book, I’ll give you a thorough overview of critical computer science topics such as machine learning, regular expression, data science, NumPy, and Python basics—all in a single line of Python code!

Get the book from Amazon!

OFFICIAL BOOK DESCRIPTION: Python One-Liners will show readers how to perform useful tasks with one line of Python code. Following a brief Python refresher, the book covers essential advanced topics like slicing, list comprehension, broadcasting, lambda functions, algorithms, regular expressions, neural networks, logistic regression and more. Each of the 50 book sections introduces a problem to solve, walks the reader through the skills necessary to solve that problem, then provides a concise one-liner Python solution with a detailed explanation. 