# How to 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 list
np.std(my_list)
```

Method 2: Use statistics Library

```import statistics as stat

#calculate standard deviation of list
stat.stdev(my_list)```

Method 3: Use Custom Formula

```#calculate standard deviation of list
st.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 list
np.std(my_list, ddof=1)

5.310367218940701

#calculate population standard deviation of list
np.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 list
stat.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)))**2 for 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)))**2 for 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.

May 13, 2024
April 25, 2024
April 19, 2024
April 18, 2024
April 18, 2024

## One Reply to “How to Calculate the Standard Deviation of a List in Python”

1. Sevin says:

How do I print* the standard deviation?