Python Program to Calculate the Average of List Items

Write a Python program to calculate the average of list items using for loop, sum function, user defined, mean, and reduce functions. In this example, we use the sum() and len() functions to divide the list total with total items.

avlst = [10, 20, 90, 30, 40, 50]

avg = sum(avlst) / len(avlst)

print('The Result = ', avg)
The Result =  40.0

This example allows entering list items and calculates the sum and average using the sum function.

avglist = []
Number = int(input("Total Number of List Items = "))

for i in range(1, Number + 1):
    value = int(input("Please enter the %d List Item = "  %i))
    avglist.append(value)

total = sum(avglist)
avg = total / Number

print('\nThe Sum Of List Items     = ', total)
print('\nThe Average Of List Items = ', avg)
Python Program to Calculate the Average of List Items

Write a Program to find List Average in Python using for loop

This program uses for loop to iterate the items and calculates the sum and average of all list elements.

avglist = []
total = 0

Number = int(input("Total Number of List Items = "))

for i in range(1, Number + 1):
    value = int(input("Please enter the %d List Item = "  %i))
    avglist.append(value)

for i in range(Number):
    total = total + avglist[i]

avg = total / Number

print('\nThe Sum Of List Items     = ', total)
print('\nThe Average Of List Items = ', avg)
Total Number of List Items = 7
Please enter the 1 List Item = 22
Please enter the 2 List Item = 13
Please enter the 3 List Item = 43
Please enter the 4 List Item = 67
Please enter the 5 List Item = 98
Please enter the 6 List Item = 11
Please enter the 7 List Item = 26

The Sum Of List Items     =  280

The Average Of List Items =  40.0

The above example uses the for loop range function. However, you can use a simple for loop to calculate the list average.

avlst = [17, 25, 85, 90, 33, 40, 50]

tot = 0

for i in avlst:
    tot += i

avg = tot / len(avlst)

print('The Result = ', avg)
The Result =  48.57142857142857

Python Program to Calculate the Average of List Items using functions

In this example, the listAverage function accepts a list and returns the sum and average of all items.

def listAverage(avglist):
    total = 0
    for i in range(Number):
       total = total + avglist[i]
    avg = total / len(avglist)
    return total, avg


avglist = []

Number = int(input("Total Number of Items = "))

for i in range(1, Number + 1):
    value = int(input("Please enter the %d List Item = "  %i))
    avglist.append(value)

total, avg = listAverage(avglist)

print('\nThe Sum     = ', total)
print('\nThe Average = ', avg)
Total Number of Items = 8
Please enter the 1 List Item = 2
Please enter the 2 List Item = 9
Please enter the 3 List Item = 11
Please enter the 4 List Item = 24
Please enter the 5 List Item = 58
Please enter the 6 List Item = -11
Please enter the 7 List Item = 17
Please enter the 8 List Item = 99

The Sum     =  209

The Average =  26.125

Python Program to find the Average of List Items using built-in methods

In this programming language, there are many options to calculate the list average, and this section covers most of them.

Using mean and reduce functions

In this Python program, we imported the mean from the statistics module and reduce method from the functools module. Both these functions will calculate the average of list items.

from statistics import mean
from functools import reduce

avglist = []
Number = int(input("Total Number of List Items = "))

for i in range(1, Number + 1):
    value = int(input("Please enter the %d List Item = "  %i))
    avglist.append(value)

total = sum(avglist)
avg1 = mean(avglist)
avg2 = reduce(lambda x, y: x + y, avglist) / Number
#avg2 = reduce(lambda x, y: x + y, avglist) /len(avglist)

print('\nThe Sum     = ', total)
print('\nThe Average = ', avg1)
print('\nThe Average = ', avg2)
Total Number of List Items = 6
Please enter the 1 List Item = 19
Please enter the 2 List Item = 98
Please enter the 3 List Item = 56
Please enter the 4 List Item = 49
Please enter the 5 List Item = 77
Please enter the 6 List Item = 67

The Sum     =  366

The Average =  61

The Average =  61.0

Using the numpy mean function

In this example, first, we have to import the numpy module, so we can use the mean function.

import numpy as np

avlst = [98, 44, 36, 89, 11, 14]

avg = np.mean(avlst)

print('The Result = ', avg)
The Result =  48.666666666666664

Python List Average using lambda function

avlst = [10, 20, 30, 40, 50, 60, 70]

avg = (lambda n: sum(n) / len(n))(avlst)

print('The Result = ', avg)
The Result =  40.0

Using generator expression

avlst = [10, 20, 30, 40, 50, 60]

avg = sum(n for n in avlst) / len(avlst)

print('The Result = ', avg)
The Result =  35.0