4 Ways to Find Average of List in Python: A Complete Guide

A short one-liner script that calculates the average of a list in Python

To find the average of a list in Python:

  1. Sum up the elements of a list.
  2. Divide the sum by the length of the list.

For instance, let’s calculate an average of a list of numbers:

grades = [4, 3, 3, 2, 5]

average = sum(grades) / len(grades)

print(f"The average is {average}")

Output:

The average is 3.4

This is a comprehensive guide to calculating the average of a list in Python. In this guide, you learn four separate ways for calculating averages.

  1. The for-loop approach
  2. The statistics mean() function
  3. The numpy mean() function
  4. The reduce() function

4 Ways to Calculate the Average of a List in Python

Let’s take a closer look at four different ways to calculate the average of a list in Python.

1. The For Loop Approach

A python for loop that calculates the average of a list

The basic way to calculate the average of a list is by using a loop.

In this approach, you sum up the elements of the list using a for loop. Then you divide the sum by the length of the list.

For example, let’s calculate the average of grades using a for loop:

grades = [4, 3, 3, 2, 5]

sum = 0
for number in grades:
    sum += number

average = sum / len(grades)

print(f"The average is {average}")

Output:

The average is 3.4

2. Statistics mean() Function

A script that calculates the average of a Python list using mean() function

Python has a built-in library called statistics. This library provides basic functionality for mathematics in statistical analysis.

One of the useful functions provided by the statistics library is the mean() function. You can use it to calculate the average for a list.

For example:

from statistics import mean

grades = [4, 3, 3, 2, 5]

average = mean(grades)

print(f"The average is {average}")

Output:

The average is 3.4

3. NumPy mean() Function

Numpy mean() function to calculate list average in Python

NumPy is a commonly used Python library in data science. It provides you with powerful numerical computing tools and multi-dimensional arrays.

This library has also its own implementation of the mean() function.

To use it, first install NumPy by running the following command in your command line window:

pip install numpy

With Numpy installed, you can use the mean() function to calculate the average of a list:

from numpy import mean

grades = [4, 3, 3, 2, 5]

average = mean(grades)

print(f"The average is {average}")

Output:

The average is 3.4

Notice that installing and using the mean() function from NumPy is overkill if you’re never going to use the NumPy library again. But if you’re already using it in your project, then calculating the average with it might make sense.

4. reduce() Function in Python

Using the reduce() function to calculate average of a list in Python

Python’s functools library has a function called reduce(). You use this function when computing the average of a list by calculating the sum of the list.

Notice that using reduce in Python isn’t recommended anymore as better solutions exist. Make sure to read my article about the reduce() function to understand how it works and why it’s not used anymore.

Anyway, for the sake of demonstration, let’s use the reduce() function to calculate the average of a list:

from functools import reduce

grades = [4, 3, 3, 2, 5]

average = reduce(lambda x, y: x + y, grades) / len(grades)

print(f"The average is {average}")

Output

The average is 3.4

The reduce() function works by applying an operation on two elements of a list. It remembers the result and applies the operation to the next element and the result. It does this to accumulate a result for the whole list.

The operation in this example is the lambda expression lambda x, y: x + y. This is nothing but a function that takes two arguments x and y and returns the sum.

When we call reduce() on a list using this lambda, we tell it to sum up the numbers of a list.

Further Reading