Open In App

Find average of a list in python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a list of numbers, the task is to find the average of that list. Average is the sum of elements divided by the number of elements.

Input : [4, 5, 1, 2]
Output : 3
Explanation:
Sum of the elements is 4+5+1+2 = 12 and total number of elements is 4.
So average is 12/4 = 3

Input : [15, 9, 55]
Output : 26.33
Explanation:
Sum of the elements is 15+9+53 = 77 and total number of elements is 3.
So average is 77/3 = 26.33

Average of a List using sum() and len() in Python

In Python, we can find the average of a list by simply using the sum() and len() functions.

  • sum(): Using sum() function we can get the sum of the list.
  • len(): len() function is used to get the length or the number of elements in a list.
Python3
# Python program to get average of a list 
def Average(lst): 
    return sum(lst) / len(lst) 

# Driver Code 
lst = [15, 9, 55, 41, 35, 20, 62, 49] 
average = Average(lst) 

# Printing average of the list 
print("Average of the list =", round(average, 2)) 

Output:

Average of the list = 35.75

Time Complexity: O(n) where n is the length of the list.
Auxiliary Space: O(1) as we only require a single variable to store the average.

Average of a List using reduce() and lambda in Python

We can use the reduce() to reduce the loop and by using the lambda function can compute the summation of the list. We use len() to calculate length as discussed above. 

Python3
# Python program to get average of a list 
# Using reduce() and lambda 

# importing reduce() 
from functools import reduce

def Average(lst): 
    return reduce(lambda a, b: a + b, lst) / len(lst) 

# Driver Code 
lst = [15, 9, 55, 41, 35, 20, 62, 49] 
average = Average(lst) 

# Printing average of the list 
print("Average of the list =", round(average, 2)) 

Output:

Average of the list = 35.75

Time complexity: O(n), where n is the length of the list lst.
Auxiliary space: O(1). The space used is constant and independent of the size of the input list.

Average of a List using Python mean()

The inbuilt function mean() can be used to calculate the mean( average ) of the list.

Python3
# Python program to get average of a list 
# Using mean() 

# importing mean() 
from statistics import mean 

def Average(lst): 
    return mean(lst) 

# Driver Code 
lst = [15, 9, 55, 41, 35, 20, 62, 49] 
average = Average(lst) 

# Printing average of the list 
print("Average of the list =", round(average, 2)) 

Output:

Average of the list = 35.75

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1).

Average of a List by iterating List in Python

Iterating lists using for loop and doing operations on each element of the list.

Python3
# Python code to get average of list
def Average(lst):
    sum_of_list = 0
    for i in range(len(lst)):
        sum_of_list += lst[i]
    average = sum_of_list/len(lst)
    return average


# Driver Code
lst = [15, 9, 55, 41, 35, 20, 62, 49]
average = Average(lst)
print("Average of the list =", round(average, 2))

Output:

Average of the list = 35.75

Time complexity: O(n) 
Auxiliary space: O(n), where n is the length of list.

Average of a List using Python numpy.average() function 

We can find the average of a list in Python by using average() function of NumPy module.

Python3
# importing numpy module
import numpy

# function for finding average
def Average(lst):
    # average function
    avg = numpy.average(lst)
    return(avg)


# input list
lst = [15, 9, 55, 41, 35, 20, 62, 49]

# function call
print("Average of the list =", round(Average(lst), 2))

Output:

Average of the list = 35.75


Last Updated : 14 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads