Python Program to Find the Sum and Average of a List

In this article, we will show how to write a Python program to find the sum and average of a list of elements with examples. There are multiple approaches to finding the sum and average of the list, including the built-in functions like sum(), mean(), etc, and loops.

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

The first approach is using the len() function to find the total items in a list and the sum() method to calculate the sum of list elements. In the program below, we have created an integer list with eight values. Here, sum(a) returns the sum of the eight items in a list. Next, total / len(a) will calculate the list average.

a = [5, 10, 15, 25, 40, 65, 100, 150]

total = sum(a)
avg = total / len(a)

print("Sum = ", total)
print("Average = ", avg)
Sum      =  410
Average  =  51.25

The program below allows the user to enter the total number of list items and their values. Next, this Python program uses the above sum() function technique to find the sum and average of the list items. You can apply this procedure in all the examples to allow the user to decide the list size and values.

a = []
n = int(input("Enter the Total Number of List Elements : "))

for i in range(n):
value = int(input("Enter Element : "))
a.append(value)

total = sum(a)
avg = total / n

print("Sum = ", total)
print("Average = ", avg)
Python Program to Find the Sum and Average of a List

Python Program to Find the Sum and Average of a List using for loop

If you don’t want to use any built-in functions, a for loop is the best and most popular approach to find the sum and average of the total elements in a list. 

As you all know, the sum means adding all items using the + operator, and the average is the sum divided by the total number of items. Here, the for loop will iterate every element in a list. Within the loop, the total += n (total = total + n) line will add each element to the total. The next line count += 1 (count = count + 1) will increment or add 1 for each iteration. It means counting the total number of list items. 

a = [50, 10, 70, 25, 40, 65, 100, 150]

total = count = 0

for n in a:
total += n
count += 1

avg = total / count

print("Sum = ", total)
print("Average = ", avg)
Sum      =  510
Average  =  63.75

Using a while loop

Instead of a For loop, this Python program uses the While loop to iterate the list items and calculate the sum and average of the list elements.

a = [50, 10, 70, 25, 40, 65, 100, 150]

total = 0
length = len(a)

i = 0
while i < length:
total += a[i]
i += 1

avg = total / length

print("Sum = ", total)
print("Average = ", avg)
Sum      =  510
Average  =  63.75

Python Program to Find the Sum and Average of a List using modules

The examples below use the built-in functions that are available in different modules.

Using statistics

In Python, the statistics module has two helpful functions fsum() and mean(). The fsum() is useful to find the sum of the list items, and the mean(() will calculate the average of them. In the program below, we use both of them.

import statistics

a = [50, 10, -70, 140, 65, 100, 150]

total = statistics.fsum(a)
avg = statistics.mean(a)

print("Sum = ", total)
print("Average = ", avg)
Sum      =  445.0
Average  =  63.57142857142857

Using numpy module

Apart from the above options, you can try the available built-in function in the popular NumPy module. In this Python program, we imported the numpy module and invoked the sum() and average() functions to find or calculate the sum and average of the list elements. You can also use the numpy mean() function to find the average.

import numpy as np

a = [50, 10, -70, 140, 65, 100, 150]

total = np.sum(a)
avg = np.average(a)

print("Sum = ", total)
print("Average = ", avg)
Sum      =  445
Average  =  63.57142857142857

Python Program to Find the Sum and Average of a List using math module

The math module has the built-in function called the fsum() to calculate the sum of the given items. In this program, we use the math fsum() function to calculate the sum and average of the total elements or items in a list.

import math

a = [50, 140, 65, 110, 75, 100, 150]

total = math.fsum(a)

avg = total / len(a)

print("Sum = ", total)
print("Average = ", avg)
Sum      =  690.0
Average  =  98.57142857142857

Using reduce() function

Within the functools there is a reduce() method, and you can use it as well. The reduce() function is helpful to apply a particular function or calculation to the set of elements passed to the function. Here, we used the reduce function along with the lambda expression to find the sum and average of list items. 

from functools import reduce

a = [50, 140, 65, 110, 75, 100, 150]

total = reduce(lambda x, y: x + y, a)

avg = total / len(a)
# avg = reduce(lambda x, y: x + y, a) / len(a)

print("Sum = ", total)
print("Average = ", avg)
Sum      =  690
Average  =  98.57142857142857

Using add() in the operator module

The operator module has the add function to add the total items in a given object. Here, we use the operator add() method in combination with the reduce() function. So, it will assign the add method to the complete list.

from functools import reduce
import operator

a = [50, 140, 65, 110, 75, 100, 15]

total = reduce(operator.add, a)

avg = total / len(a)

print("Sum = ", total)
print("Average = ", avg)
Sum      =  555
Average  =  79.28571428571429