Python Program to Calculate the Average of List Items

Write a Python program to calculate the average of list items. 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

In this python program, we imported the mean from the statistics module and reduce 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

This python 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

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