Find the mean of a list python

Prerequisites: sum[] function, len[] function, round[] function, reduce[], lambda, and mean[]. Given a list of numbers, the task is to find average of that list. Average is the sum of elements divided by the number of elements.

Input : [4, 5, 1, 2, 9, 7, 10, 8]
Output : Average of the list = 5.75
Explanation:
Sum of the elements is 4+5+1+2+9+7+10+8 = 46
and total number of elements is 8.
So average is 46 / 8 = 5.75

Input : [15, 9, 55, 41, 35, 20, 62, 49]
Output : Average of the list = 35.75
Explanation:
Sum of the elements is 15+9+55+41+35+20+62+49 = 286
and total number of elements is 8.
So average is 46 / 8 = 35.75

Using sum[]

In Python we can find the average of a list by simply using the sum[] and len[] function.

  • 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

def Average[lst]:

    return sum[lst] / len[lst]

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

Using reduce[] and lambda

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

Python3

from functools import reduce

def Average[lst]:

    return reduce[lambda a, b: a + b, lst] / len[lst]

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

Using mean[]

The inbuilt function mean[] can be used to calculate the mean[ average ] of the list. 

Python3

from statistics import mean

def Average[lst]:

    return mean[lst]

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

By iterating list

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

Python3

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

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


How do you find the mean of a list in Python?

There are two ways to find the average of a list of numbers in Python. You can divide the sum[] by the len[] of a list of numbers to find the average. Or, you can find the average of a list using the Python mean[] function.

How do you print the average of a list in Python?

Take a look at the code below:.
def Average[l]: avg = sum[l] / len[l] return avg. my_list = [2,4,6,8,10] average = Average[my_list] ... .
from statistics import mean. ​ def Average[l]: avg = mean[l] return avg. ... .
from functools import reduce. ​ def Average[l]: avg = reduce[lambda x, y: x + y, l] / len[l] return avg..

How do you find the average of a list?

Summary: The formula to calculate average is done by calculating the sum of the numbers in the list divided by the count of numbers in the list.

How do you find the average value in Python?

Python mean[] function mean[] function is used to calculate the mean/average of input values or data set. The mean[] function accepts the list, tuple or data-set containing numeric values as a parameter and returns the average of the data-items. In the above snippet of code, we have used statistics.

Chủ Đề