How to take the average of a list in Python

Key takeaways:

  • Python offers multiple ways to calculate the average of a list.

  • The average is computed by dividing the sum of all numbers in a list by length.

  • Using sum() and len() is straightforward for calculating the average because they are built-in, efficient, and provide clear, concise code for summing and counting list elements.

  • The mean() function from the statistics module directly computes the average.

  • The reduce() method combined with a lambda function can also calculate the average.

  • Each method provides flexibility depending on the complexity and requirements of your task.

Calculating the average is important because it provides a simple and effective way to summarize a dataset, helping to identify trends, make comparisons, and support data-driven decisions across various fields such as statistics, data analysis, and machine learning.

In this Answer, we will explore how we can take a list's average in Python. As we know, Python is a very robust programming language that offers high-level control over its data structures. So, let’s dive right into finding the average of a list!

Let’s first review how to calculate the average of a list. The average is the sum of all the numbers in a list divided by length.

We have a list of numbers that we want to take an average of
We have a list of numbers that we want to take an average of
1 of 4

In Python, we can perform several operations to get to our desired output. The following approaches can be used to compute the average for our list.

Methods to compute the average

Let’s examine a few of the approaches used to compute the average for a list of numbers.

1. Using sum() and len() functions

An average can be computed using the sum() and len() functions on the list. The sum() function will return the sum of all the values in the list, which can be divided by the number of elements returned by the len() function. Let’s go over the code below:

def Average(l):
avg = sum(l) / len(l)
return avg
my_list = [2,4,6,8,10]
average = Average(my_list)
print("Average of my_list is", average)

Let’s look at the code explanation:

  • Line 2: The sum() function sums the elements in the list, and the len() function returns the length of the list.

  • Line 3: After dividing the two returned numbers, we can get the list’s average.

2. Using the statistics.mean() function

The mean() function in the python statistics library can be used to directly compute the average of a list. Let’s go over how we can use this function:

from statistics import mean
def Average(l):
avg = mean(l)
return avg
my_list = [2,4,6,8,10]
average = Average(my_list)
print("Average of my_list is", average)

In the code shown above:

  • Line 4: Using the mean() function, we can compute the average for our list, l.

3. Using reduce() and Lambda functions

The reduce method can loop through the list and the sum can be computed in the Lambda function. The number of elements can be obtained using len().

from functools import reduce
def Average(l):
avg = reduce(lambda x, y: x + y, l) / len(l)
return avg
my_list = [2,4,6,8,10]
average = Average(my_list)
print "Average of my_list is", average

Here’s a brief explanation of the code:

  • Line 3: We calculate the average by summing all elements of l using reduce with a lambda function (x + y) and dividing by the list’s length.

4. Using a for loop

In this approach, we manually iterate over each element in the list using a for loop, adding each element to a total variable. After the loop ends, we divide the total by the number of elements (len(my_list)) to compute the average.

my_list = [10, 20, 30, 40, 50]
total = 0
for num in my_list:
total += num
average = total / len(my_list)
print("Average using for loop:", average)

In the code shown above:

  • Lines 5–7: We can compute the average for our list by simply using the for loop.

5. Using numpy.average

The numpy.average() function is a built-in NumPy function that computes an array or list’s average (or mean). It’s a simple one-liner and highly efficient for numerical operations.

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

In the code shown above:

  • Line 4: By simply using the np.average() function, we can compute the average for our list.

6. Using pandas.Series.mean

If your data is stored in a pandas series (a one-dimensional array with labels), you can calculate the average directly using the mean() method. This is very commonly used in data analysis with pandas.

import pandas as pd
my_list = [10, 20, 30, 40, 50]
series = pd.Series(my_list)
average = series.mean()
print("Average using series.mean:", average)

In the code shown above:

  • Line 5: By simply using the series.mean() function, we can compute the average for our list.

Learn the basics with our engaging “Learn Python” course!

Start your coding journey with Learn Python, the perfect course for beginners! Whether exploring coding as a hobby or building a foundation for a tech career, this course is your gateway to mastering Python—the most beginner-friendly and in-demand programming language. With simple explanations, interactive exercises, and real-world examples, you’ll confidently write your first programs and understand Python essentials. Our step-by-step approach ensures you grasp core concepts while having fun. Join now and start your Python journey today—no prior experience is required!

Conclusion

Python provides multiple efficient ways to compute the average of a list, offering flexibility to choose the method that best suits your coding style and requirements. From using simple built-in functions like sum() and len() for clarity and ease to leveraging libraries like statistics.mean() for built-in solutions and even utilizing more functional approaches such as reduce() with lambda for advanced use cases, Python’s versatility ensures that developers have the right tools for the job. By exploring these approaches, you can choose the most appropriate method based on readability, performance, or functionality, making Python a robust choice for data manipulation tasks.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do you find the average of a list in pandas?

Use df['column_name'].mean() to find the average of a list in a pandas DataFrame column.


Is the avg() a function in Python?

No, avg() is not a built-in function in Python; use mean() from the statistics module or calculate manually.


How to get the median in Python

Use the median() function from the statistics module, e.g., median_value = statistics.median([1, 2, 3, 4, 5]).


How to make a list in Python

Use square brackets [] to create a list in Python, e.g., my_list = [1, 2, 3].


How to average two lists in Python

To average two lists element-wise in Python, you can zip the lists together, sum the corresponding elements, and then divide by 2. Here’s how you can do it:

Method 1: Using a for loop
list1 = [2, 3, 4]
list2 = [4, 5, 6]

average_list = [(a + b) / 2 for a, b in zip(list1, list2)]
print(average_list)  # Output: [3, 4, 5]
  • zip(list1, list2): Pairs corresponding elements from both lists.
  • (a + b) / 2: Calculates the average of the paired elements.
Method 2: Using NumPy (for larger data sets)

If you have large lists, using NumPy can make the process more efficient.

import numpy as np

list1 = [1, 2, 3]
list2 = [4, 5, 6]

average_list = np.mean([list1, list2], axis=0)
print(average_list)  # Output: [2.5 3.5 4.5]
  • np.mean(): A NumPy function that computes the mean across the specified axis.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved