The operator.add() function can be used to calculate the summation of all the data values present in the list with the help of Python reduce() function. The Python operator module contains various functions to perform basic calculations and operations efficiently. Python operator.add() function to find the average of a list Print ( "Average value of the list:\n" ) print (lst_avg ) print ( "Average value of the list with precision upto 3 decimal value:\n" ) print ( round (lst_avg, 3 ) )Ĥ. Lst_avg = reduce ( lambda x, y : x + y, inp_lst ) /lst_len Python lambda() function: The lambda() function is used to build and form Anonymous functions i.e. Finally, the result is returned to the terminal/screen as output.This process continues until it reaches the end of the list.Further, we apply the same function to the result obtained in the previous step and the element succeeding the second element.Initially, the reduce() function applies the passed function to the first two consecutive elements and returns the result.Syntax: reduce (function, input - list /sequence ) Python reduce() function: The reduce() function is basically used to apply a particular(input) function to the set of elements passed to the function. We can use Python reduce() function along with the lambda() function. Lst_avg = sum_lst / len (inp_lst ) print ( "Average value of the list:\n" ) print (lst_avg ) print ( "Average value of the list with precision upto 3 decimal value:\n" ) print ( round (lst_avg, 3 ) )ģ. the count of data items present in the list.įurther, statistics.sum() function is used to calculate the sum of all the data items in the list. The statistics.len() function is used to calculate the length of the list i.e. Python statistics.sum()function can also be used to find the average of data values in Python list. Output: Average value of the list : 67.51375Īverage value of the list with precision upto 3 decimal value : 67.514 In the above snippet of code, we have used statistics.round() method to round off the output average up to a particular decimal value. List_avg = mean (inp_lst ) print ( "Average value of the list:\n" ) print (list_avg ) print ( "Average value of the list with precision upto 3 decimal value:\n" ) print ( round (list_avg, 3 ) ) Syntax: mean (data - set / input -values )
The mean() function accepts the list, tuple or data-set containing numeric values as a parameter and returns the average of the data-items. The an() function is used to calculate the mean/average of input values or data set. Python 3 has statistics module which contains an in-built function to calculate the mean or average of numbers. Techniques to find the average of a list in PythonĮither of the following techniques can be used to calculate the average/mean of a list in Python: In general, an average is a value that represents a whole set of data items or elements.įormula: Average = summation of numbers/total count. Hi Folks! In this article, we will have a look at the various ways to find the average of a list in a Python List.