Python Program to Calculate Average of Numbers in List

We will develop a Python program to calculate the average of numbers in a given list. The average is defined as the mean value which is equal to the ratio of the sum of the number of a given set of values to the total number of values present in the set. Here, we will calculate the average of n numbers using various methods.

How to find the average of numbers:

Average Formula = Total sum of all numbers / Number of item in the set
Average = (x1+x2+x3+…+xn)/n

Mathematically,

list = [5, 10, 15, 20, 25]
Average = (5+10+15+20+25)/5 = 75/5 = 15

Python Program for Average of Numbers in a List

In this program, we are using the For Loop to calculate the sum of numbers in a list. we can also take the help of a function to find the average of numbers in a list. A function is a block of code that performs a specific task. len() function is used to get the length or the number of elements in a list.

# Python program to calculate the average of numbers in a given list

def find_Average(n):
    sum_num = 0
    for i in n:
        # calculate sum of numbers in list
        sum_num = sum_num + i        
    
    # calculate average of numbers in list
    avg = sum_num / len(n)
    return avg

# take list
l = [5, 3, 8, 20, 15]

# calling function and display result
print('The average of list = %0.2f' %find_Average(l))

Output:-

The average of list = 10.20

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

def find_Average(n):
    sum_num = 0
    for i in n:
        sum_num = sum_num + i        
    
    avg = sum_num / len(n)
    return avg

# total number you want to enter
n = int(input('How many numbers: '))

#take list
l = []
for i in range(n):
    l.append(float(input('Enter number: ')))

print('The average of list = %0.2f' %find_Average(l))

Output:-

How many numbers: 3
Enter number: 10
Enter number: 20
Enter number: 25
The average of list = 18.33

Python Average Function

We can calculate the average of numbers in the list by simply using the sum() and len() function. sum() will return the sum of all the numbers in the list, which can be divided by the number of elements returned by the len() function.

# Python program to calculate the average of numbers in a given list

def find_Average(n):
    # calculate average of numbers in list
    return sum(n) / len(n)

# take list
l = [4, 3, 15.5, 20, 17]

# calling function and display result
print('The average of list = %0.2f' %find_Average(l))

Output:-

The average of list = 11.90

Python Mean of List

The mean() function in the python statistics library can be used to directly compute the average of a list.

# Python program to calculate the average of numbers in a given list

#importing mean() function
from statistics import mean

def find_Average(n):
    # calculate average or mean
    return mean(n)

# take list
l = [25, 50, 14, 63, 48, 53]

# calling function and display result
print('The average of list = %0.2f' %find_Average(l))

Output:-

The average of list = 42.17

Python Average of List using reduce() and lambda

The reduce() to reduce the loop and by using the lambda function can compute summation of the list. The reduce() function is basically used to apply a particular(input) function to the set of elements passed to the function.

# Python program to calculate the average of numbers in a given list

#importing reduce() function
from functools import reduce

def find_Average(n):
    # calculate average of numbers in list
    return reduce(lambda x, y: x + y, n) / len(n)

# take list
l = [15, 13, 17, 2, 17]

# calling function and display result
print('The average of list = %0.2f' %find_Average(l))

Output:-

The average of list = 12.80

Python Average of Numbers in a List using numpy.average() methods

The numpy.average() method is used to calculate the average of numbers in the list.

# Python program to calculate the average of numbers in a given list

#importing numpy() module
import numpy

def find_Average(n):
    # calculate average of numbers in list
    return numpy.average(n)

# take list
l = [2, 5, 10, 21, 4, 25]

# calling function and display result
print('The average of list = %0.2f' %find_Average(l))

Output:-

The average of list = 11.17

Also See:- How to find the average of 3 numbers in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *