Python Program to Find the Average of a List

In this article, we will show how to write a Python program to find the average of integer list elements with examples. In this programming language, there are multiple approaches to calculating the list average, including the built-in len(), sum(), statistics mean(), numpy average(), reduce, and loops. 

Python Program to Find the Average of a List using len() and sum()

In the below program, we have declared an integer list of six elements. Next, we used the len() function to find the list length and then sum() to calculate the total. As we know, dividing the total by the number of list items gives the average.

a = [10, 12, 14, 16, 18, 20]
print(a)

avg = sum(a) / len(a)

print(avg)
[10, 12, 14, 16, 18, 20]
15.0

Using statistics

This program uses the mean() function available in the statistics module to find the average of the given list items.

import statistics

a = [10, 22, 44, 66, 88, 100]

avg = statistics.mean(a)

print(avg)
55

Using numpy average()

The numpy module has the built-in average() function to find the average of the given list object and we use the same to calculate avg.

import numpy

a = [10, 12, 14, 16, 18, 20]

avg = numpy.average(a)

print(avg)
15.0

Using reduce

In this program, we used lambda to find the list sum and divided it by the length to find the average. 

from functools import reduce

a = [10, 12, 14, 16, 18, 20]

length = len(a)

avg = reduce(lambda x, y: x + y, a) / length

print(avg)
15.0

It is the same as the above example but instead of lambda, use the add() function available in the operator module.

from functools import reduce
import operator

a = [10, 12, 14, 16, 18, 20, 50]

length = len(a)

avg = reduce(operator.add, a) / length

print(avg)
20.0

Python Program to Find the Average of a List Using For Loop

Apart from the above-mentioned built-in approaches, you can use the for loop to find the list average. In the below program, the for loop will iterate the integer list elements to calculate the sum. Next, divide it by length to find the list average. 

a = [10, 12, 14, 16, 18, 20]

tot = 0

for val in a:
tot = tot + val

avg = tot / len(a)

print(avg)
Python Program to Find the Average of a List

Using a while loop

Instead of a For loop, this program uses the while loop to calculate the average of the list elements.

a = [10, 22, 44, 66, 88, 100]

tot = 0
i = 0
length = len(a)

while i < length:
tot = tot + a[i]
i += 1

avg = tot / length
print(avg)
55.0