Open In App

Standard deviation of list - Python

Last Updated : 12 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

We are given a list of numbers, and our task is to compute the standard deviation. Standard deviation is a measure of how spread out the numbers are from the mean. A low standard deviation means the values are close to the mean, while a high standard deviation indicates the values are spread out. For example, given the list [10, 12, 23, 23, 16, 23, 21, 16], we need to calculate its standard deviation.

Using statistics.stdev()

We can use the stdev() function from Python's statistics module to calculate the standard deviation of a list. This method works well for small datasets.

Python
import statistics

# List of numbers
a = [10, 12, 23, 23, 16, 23, 21, 16]

# Calculate standard deviation
b = statistics.stdev(a)

print(b)

Output
5.237229365663818

Explanation: statistics.stdev(a) computes the sample standard deviation. This method works well for small datasets.

Using numpy.std()

If we are working with large lists, the numpy library provides the std() function, which calculates the population standard deviation by default, but we can adjust it for sample standard deviation by setting ddof=1.

Python
import numpy as np

# List of numbers
a = [10, 12, 23, 23, 16, 23, 21, 16]

# Calculate standard deviation
b = np.std(a, ddof=1)

print(b)

Output
5.237229365663817

Explanation:

  • np.std(a, ddof=1) computes the sample standard deviation.
  • It is optimized for numerical operations on large datasets.

Using pandas.Series.std()

For data analysis tasks, the pandas library is often used, which provides a convenient std() method to compute the standard deviation of a list or column in a DataFrame.

Python
import pandas as pd
a = [10, 12, 23, 23, 16, 23, 21, 16]

# Convert list to pandas Series
series = pd.Series(a)

# Calculate standard deviation
b = series.std()
print(b)

Output
5.237229365663817

Explanation:

  • We convert the list to a pandas.Series.
  • The .std() method is used to compute the sample standard deviation.

Practice Tags :

Similar Reads