How to Find the Average of a List in Python

By
Bosko Marijan
Published:
October 29, 2025
Topics:

Calculating the average of a list in Python is a common task in data analysis, statistics, and everyday programming. Whether you are analyzing user ratings, processing sensor readings, or summarizing financial data, finding the average (or mean) helps you quickly understand trends and patterns in your dataset.

In this tutorial, you will learn several different ways to calculate the average of a list in Python.

How to find the average of a list in Python - a tutorial.

Prerequisites

Option 1: Python mean() Function

The mean() function from Python's statistics module is one of the easiest ways to calculate an average. You can use it directly in the Python shell or inside a script. For this tutorial, we will use a script.

Follow the steps below:

1. Open a text editor or IDE and create a new file named average_mean.py. We will use nano:

nano average_mean.py

2. Add the code below to your script. Depending on your list type, use one of the following:

If you already have a list of numbers, use the following example:

import statistics
numbers = [10, 20, 30, 40, 50]
average = statistics.mean(numbers)
print("Average:", average)

If your data is stored in a file (for example, numbers.txt with one number per line), read it and calculate the mean:

import statistics
with open("numbers.txt") as f:
    data = [float(line.strip()) for line in f]
average = statistics.mean(data)
print("Average from file:", average)

3. Execute the script in the terminal:

python3 average_mean.py
Finding the average of a list using the mean function.

This method is fast, reliable, and ideal for simple averages or small datasets.

Option 2: In-built sum() Method

Another straightforward way is to use the built-in sum() and len() functions. This approach is minimal as it requires no imports, and it is efficient for small or medium-sized datasets.

Follow the steps below:

1. Create a new Python script named average_sum.py and paste the following code:

numbers = [10, 20, 30, 40, 50]
average = sum(numbers) / len(numbers)
print("Average:", average)

If you already have a file, modify the script as follows:

with open("numbers.txt") as f:
    numbers = [float(line.strip()) for line in f]
average = sum(numbers) / len(numbers)
print("Average from file:", average)

Replace numbers.txt with the name of your file.

2. Run the script from your terminal:

python3 average_sum.py
Finding the average in Python using the sum method.

The output shows the average from your list.

Option 3: Python lambda and reduce() Method

For users who prefer a functional programming style, you can use reduce() with a lambda function to sum the elements and then divide by the list length. This works equally well whether your list is defined in a script or loaded from a file.

Although less concise, this method is a good demonstration of how functional tools in Python can replace loops and manual accumulation.

Follow the steps below:

1. Create a new file and paste the following code:

from functools import reduce
numbers = [10, 20, 30, 40, 50]
total = reduce(lambda x, y: x + y, numbers)
average = total / len(numbers)
print("Average:", average)

If your list is stored in a file, adjust the script like this:

from functools import reduce
with open("numbers.txt") as f:
    numbers = [float(line.strip()) for line in f]
total = reduce(lambda x, y: x + y, numbers)
average = total / len(numbers)
print("Average from file:", average)

2. Save the file as average_reduce.py.

3. Run the script with:

python3 average_reduce.py
Finding the average of a list in Python using the reduce method.

This method is helpful when working with functional constructs or combining calculations.

Option 4: Python operator.add() Method

Using the operator module provides a cleaner and slightly faster way to perform the same operation as the lambda version. By combining operator.add() with reduce(), you get both readability and efficiency.

Follow the steps below:

1. Create a Python script and paste the following code:

from functools import reduce
import operator
numbers = [10, 20, 30, 40, 50]
total = reduce(operator.add, numbers)
average = total / len(numbers)
print("Average:", average)

If you are working with an existing file, update the script as follows:

from functools import reduce
import operator
with open("numbers.txt") as f:
    numbers = [float(line.strip()) for line in f]
total = reduce(operator.add, numbers)
average = total / len(numbers)
print("Average from file:", average)

2. Save the file as average_operator.py.

3. Execute the script:

python3 average_operator.py
Finding the average of a list with the add operator in Python.

This approach is cleaner and slightly faster than the lambda version, especially for large datasets.

Option 5: NumPy average() Method

If you are handling numerical data or large datasets, such as CSV files, sensor data, or database exports, NumPy's average() method is the most efficient solution. NumPy arrays are optimized for mathematical operations and can handle millions of elements quickly.

Follow the steps below:

1. Install NumPy if you do not already have it. Run:

pip install numpy

Note: If you get an "externally managed" error and cannot install NumPy, it means you are not allowed to install packages globally with pip install. This is to avoid breaking system packages. Instead, create a virtual environment and install NumPy within that environment.

2. Create a new file and paste the following code:

import numpy as np
numbers = [10, 20, 30, 40, 50]
average = np.average(numbers)
print("Average:", average)

If your data is stored in a file, use NumPy's loadtxt() to read it:

import numpy as np
data = np.loadtxt("numbers.txt")
average = np.average(data)
print("Average from file:", average)

3. Save the file as average_numpy.py.

4. Execute the script with:

python3 average_numpy.py
Finding the average of a list in Python using NumPy.

NumPy's implementation is fast and optimized for large-scale data analysis.

Conclusion

This article showed five methods for finding the average of a list in Python, from built-in functions to optimized libraries like NumPy. For quick tasks, sum() and len() or statistics.mean() are ideal. For performance-critical applications or large datasets, NumPy offers the fastest and most scalable solution.

Next, check out our Python poetry guide or see how to delay code execution in Python.

Was this article helpful?
YesNo