You are currently viewing Average of List in Python ( 7 Examples)

How to find the average or mean of elements in the list in Python? A mean is defined as the mathematical average of two or more values. There are different ways to calculate the average of all the number elements in the list. for example, you can use the sum() built-in function along with len(). Besides this, there are several other ways to get the average of the list.

Advertisements

Below are the methods to calculate the average of elements from the list in Python.

  • Method 1: Using Looping like for loop and while loop.
  • Method 2: Using sum(), len() built-in functions
  • Method 3: Using mean() from statistics and numpy modules
  • Method 4: Using average() from numpy module
  • Method 5: Using reduce() with lambda expression and operator.add() method.

1. Quick Examples of Average of a List

Following are quick examples of getting the average or mean of elements in the list.


from functools import reduce
import statistics 
from operator import add
import numpy

# Consider the list of integers
scores=[100,200,300,400,500,120,450]
print("Actual List: ",scores)

# Example 1: Using sum() and len()
print("Average Score: ",sum(scores) / len(scores))

#  Example 2: Using lambda expression with reduce()
print("Average Score: ",reduce(lambda i, j: i + j, scores) / len(scores))

#  Example 4: Using add() from operator module with reduce()
print("Average Score: ",reduce(add, scores)/len(scores))

#  Example 3: Using mean() from statistics module
print("Average Score: ",statistics.mean(scores))

#  Example 5: Using mean() from numpy module
print("Average Score: ",numpy.mean(scores))

#  Example 6: Using average() from numpy module
print("Average Score: ",numpy.average(scores))

# Example 7: Using for loop
total=0
for i in scores:
    total=total+i
print("Average Score: ",total/len(scores))

# Example 8: Using while loop
total=0
i=0
while (i < len(scores)):
    total=total+scores[i]
    i=i+1
print("Average Score: ",total/len(scores))

2. Average of List in Python using sum() & len()

Let’s get the average of the list in Python using sum() & len() functions. In general, the average is calculated by getting the sum of the elements in the list and dividing it by the count of elements. The sum() is a Python in-built function that will return the total sum of elements from the list and the len() will return the total number of elements present in the list.

If we divide the result of sum() and the result of len() function we can get the average of elements present in the list. Both functions take the list as a parameter.

2.1 Syntax

Let’s see the syntax of how to get the average of elements in a list using sum() and len().


# Syntax
sum(mylist1 ) / len(mylist1 )

2.2 Python List Average Example

Consider the list that holds 7 integers and return the average of these elements using sum() and len().


# Consider the list of integers
scores=[100,200,300,400,500,120,450]
print("Actual List: ",scores)

# Using sum() and len()
print("Average Score: ",sum(scores) / len(scores))

This example yields the below output.

python list average

The sum of elements is 2070 and the total elements in the list are 7, So the average is 2070/7 => 295.7142857142857.

3. Using mean() from statistics module

In the above example, we have calculated the average manually by using the other methods. Let’s use the existing method to get the same result. In python, the statistics module provides a mean() function which will return the average of elements present in the list.

3.1 Syntax

Let’s see the syntax of how to return the average of elements in a list using mean() from the statistics module.


# Syntax
statistics.mean(mylist1)

Here, mylist1 is the input list.

3.2 Average List Example using mean()

Consider the list that holds 3 integers and return the average of these elements. Here, The sum of elements is 600 and the total elements in the list are 3, So the average is 600/3 => 200.


# Import module
import statistics

# Consider the list of integers
scores=[100,200,300]
print("Actual List: ",scores)

# Using mean() from statistics module
print("Average Score: ",statistics.mean(scores))

This example yields the below output.

python mean list

4. Get List Average using functools.reduce()

The reduce() function from functools module will reduce the iterable/sequence by applying a mathematical expression. Here, we will pass add() method from the operator module, which will return the sum of all elements in the list and divide it by the total number of elements in the list by using len() function.

4.1 Syntax

Let’s see the syntax of using reduce() & operator.add() methods.


# Syntax
reduce(operator.add, mulist1)/len(mulist1)

4.2 Example

Consider the list that holds 5 integers and return the average of these elements using reduce() by passing add() method as a parameter.


# Import modules
from functools import reduce
from operator import add

# Consider the list of integers
scores=[100,200,300,678,90]
print("Actual List: ",scores)

# Using reduce()  add()
print("Average Score: ",reduce(add, scores)/len(scores))

# Output:
# Actual List:  [100, 200, 300, 678, 90]
# Average Score:  273.6

5. Using reduce with Lambda Expression

Here, we will use lambda expression as a mathematical expression that will return the average of elements present in the python list. We will first add numbers and then divide this sum by the total number of elements present in the list.

5.1 Syntax

Let’s see the syntax of using lambda expression in reduce().


# Syntax
reduce(lambda i, j: i + j, mylist1) / len(mylist1)

5.2 Example

Consider the list that holds 6 integers and return the average of these elements using reduce() by passing lambda expression as a parameter.


from functools import reduce

# Consider the list of integers
scores=[1,2,3,4,5,6]
print("Actual List: ",scores)

# Using lambda expression with reduce()
print("Average Score: ",reduce(lambda i, j: i + j, scores) / len(scores))

# Output:
# Actual List:  [1, 2, 3, 4, 5, 6]
# Average Score:  3.5

6. Using numpy Module to get List Average

The NumPy library is a popular open-source library used for scientific computing applications, and it stands for Numerical Python, which is consisting of multidimensional array objects and a collection of routines for processing those arrays.

The numpy module supports two functions that will return the average of values in the list which are mean() and average(), you can use any of these to get the average of the python list.

6.1 Syntax

Let’s see the syntax of how to return the average of elements in a list using mean() from the numpy module.


# Using mean()
numpy.mean(mylist1)

# Using average()
numpy.average(mylist1)

6.2 Examples

Example 1: Consider the list that holds 3 integers and return the average of these elements using numpy.mean().


# Import numpy
import numpy

# Consider the list of integers
scores=[100,200,300]
print("Actual List: ",scores)

# Using mean() from numpy module
print("Average Score: ",numpy.mean(scores))

# Output:
# Actual List:  [100, 200, 300]
# Average Score:  200.0

Example 2: Consider the list that holds 3 integers and return the average of these elements using numpy.average().


# Import numpy
import numpy

# Consider the list of integers
scores=[100,200,300]
print("Actual List: ",scores)

# Using average() from numpy module
print("Average Score: ",numpy.average(scores))

# Output:
# Actual List:  [100, 200, 300]
# Average Score:  200.0

7. Using for loop to get List Average

Here, we will get a value from the list for each iteration done using for loop and add each element to the variable. The sum is stored in this variable. Now we will use the len() function to return the total number of elements present in the python list and divide the returned sum by the total count to get the average or mean.

7.1 Example

Consider the list that holds 3 integers and return the average of these elements using for loop


# Consider the list of integers
scores=[100,200,300]

# Using for loop
total=0
for i in scores:
    total=total+i
print("Average Score: ",total/len(scores))

# Output:
# Average Score:  200.0

8. Using a while loop

Inside the while loop, we will access each element using the index position and add each element to the total variable. The sum is stored in this variable. Each time we need to increment the iterator till it is less than the length of the list.

Now we will use the len() function to return the total number of elements present in the list and divide the returned sum by the total count returned by len() function.

8.1 Example

Consider the list that holds 3 integers and return the average of these elements using a while loop.


# Consider the list of integers
scores=[100,200,300]

# Using while loop
total=0
i=0
while (i < len(scores)):
    total=total+scores[i]
    i=i+1
print("Average Score: ",total/len(scores))

# Output:
# Average Score:  200.0

9. Conclusion

In this article, you have learned different ways to find the average of list elements in Python. We used the mean() from both the numpy and statistics module directly to return the average. And used reduce() to get the average by passing the lambda expression and operator.add() method as a mathematical expression.

Finally, we used for loop to find the average of all elements present in the list. Next, we saw how to use a while loop to find the average of elements in the list. After that, we used sum() method with len() to return the average of elements directly in the list.