What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Find Average of a List in Python: 5 Simple Methods (with Codes)

  • Nov 15, 2023
  • 7 Minutes Read
Find Average of a List in Python: 5 Simple Methods (with Codes)

In the realm of data analytics, where one needs to analyze the data through programming, Python is a versatile programming language that is simple to learn and use. Calculating the average of a list of numbers is one of the most frequent operations in this field. Here we will learn how to find the average of the list in python with code.

What does Average of a List mean?

The average of a list of numbers is the sum of all the numbers in the list divided by the number of elements in the list. it is also known as the mean of a list. It is a common statistical measure and provides a representative value for a set of numbers.

For example, if you have a list of numbers [1, 2, 3, 4, 5], the average would be (1 + 2 + 3 + 4 + 5) / 5 = 15 / 5 = 3. So, the two mathematical operations happening here are the sum of all observations and that sum being divided by the number of all observations.

The first step is to create a list of numbers. A list is a collection of values that are ordered and changeable. It is used to store multiple items in a single variable. You can learn how to print a list here.

Here, the items are all the numbers whose mean needs to be calculated. This list is created by creating a variable, assigning it square brackets [ ], and filling it with numbers separated by commas as represented below:

numbers = [1, 2, 3, 4, 5]

 

How to calculate Average of a List in Python?

There are various ways to find the mean of a list of numbers using python. We will discuss the 5 best approaches below:

1) sum and len Functions

Once we have this list of numbers, we can add up each one of them using the sum() method that is already built into python. The average can then be obtained by dividing the total by the number of elements in the list. The number of elements in a list can be determined using the "len()" function.

Predefined functions that are already included in the Python interpreter are known as built-in functions. Python is well known for its large library of predefined functions and comprehensive libraries, both of which make coding much simpler. Program writing is greatly facilitated by the use of functions like "sum()" and "len()," which ease the coding process.

Look at this example:

average = sum(numbers) / len(numbers)
print(average)

 

Output:

3.0

 

This approach is the simplest and the most efficient way of calculating the average in python using the built-in functions. There are some methods to find the list size in python as well.

But, to demonstrate the versatility of python and the many libraries it comes with, let’s look into other approaches too.

2) mean Function

One other approach to finding the average is to use the statistics module, which is a built-in module in Python that provides basic statistical functions. The statistics.mean() function can be used to calculate the average of a list. Here's the code:

import statistics
numbers = [1, 2, 3, 4, 5]
average = statistics.mean(numbers)
print(average)

 

Output:

3.0

 

3) Loop through the List

If we were to use no built-in functions like sum, another approach can be finding the sum by looping through all the elements of the list. This way to calculate the average of a list is by using a for loop. We can iterate through each element in the list and add it to a running total. Learn here, how to find index of an item in list.

After all of the elements have been processed, we can divide the total by the number of elements in the list. Look at the following code:

import statistics
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
    total += num
average = total / len(numbers)
print(average)

 

Output:

3.0

 

4) reduce Function

To calculate the average of a list in Python, we may alternatively use the reduce ()' function from the 'functools' module. The function "reduce()" applies a function cumulatively to the sequence's items after receiving a function and a sequence as parameters.

The average can be calculated by adding up all the list's components and dividing the result by the list's length using the "lambda" function.

from functools import reduce
numbers = [1, 2, 3, 4, 5]
average = reduce(lambda x, y: x + y, numbers) / len(numbers)
print(average)

 

Output:

3.0

 

For beginners, Lambda functions are generally harder to grasp so it’s understandable if the above code is not fully self-explanatory. Lambda functions are a way of writing anonymous functions without defining them as a variable. Here, the lambda function adds the parameters.

Here, The reduce() function is used to iterate the lambda function through the list essentially adding every element.

5) Using Numpy

It is also possible to use numpy library to calculate the average of a list. Numpy is a library for the Python programming language that is used for scientific computing and data analysis. To use numpy, we need to import the library and use the ‘mean()’ function to calculate the average.

import numpy as np
numbers = [1, 2, 3, 4, 5]
average = np.mean(numbers)
print(average)

 

Output:

3.0

 

One of the key benefits of Python is its vast ecosystem of libraries and frameworks that can be used to extend its functionality. For example, popular libraries such as NumPy and SciPy are used extensively in scientific computing and data analysis tasks, while Django and Flask are popular web development frameworks.

6) fsum() Function from the math Module (Python 3.8+)

Another method introduced in Python 3.8+ is using the fsum() function from the math module to calculate the sum of numbers with higher precision. By combining the fsum() function with the length of the list, we can calculate the average more accurately.

Let's calculate the average of a list [45, 34, 10, 36, 12, 6, 80] using the fsum() function from the math module.

number_list = [45, 34, 10, 36, 12, 6, 80]
avg = fsum(number_list) / len(number_list)
print("The average is", round(avg, 2))

 

Output:

The average is 31.86

 

Conclusion

You finally know all the different methods to find the average of a list in python. While there are some predefined functions and libraries, there is a simple looping method to calculate the mean.  If you’re a beginner, it's important to understand all the methods and when to use them in different situations. 

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Kusum Jain
I am an avid coder on CodeChef, I partake in hackathons regularly. I am an independent and self-motivated student with a love for coding and experiencing new things.