In previous articles, we studied about 2d lists in python, how to concatenate multiple lists, how to shuffle lists, how to reverse a list, and so much more. In this article, we will know how to find the average list in python.
The average is the number that represents the central tendency in a group of numbers. For example, if we have some numbers like – 3 4 5 6 7. The average number is the sum of all the numbers divided by the total number of numbers.
So, 5 is the average of these numbers.
In python, we have multiple ways to find the average of elements of the list. We can use built-in functions as well as by creating our functions. So, let us learn each method one by one.
Finding Average of lists in python using Loop
list1=[1,2,3,4,5,6] sum1=0 avg=0 n=len(list1) for i in list1: #calculating the sum of elements in the list sum1+=i #Average= sum1/length of list avg=sum1/n print("average of list1:",list1," is:",avg) Output- average of list1: [1, 2, 3, 4, 5, 6] is: 3.5
We can also use the sum() method directly-
list1=[1,2,3,4,5,6] Avg=sum(list1)/len(list1) print(Avg) Output- 3.5
Finding the average of lists in Python using the Numpy Library
For using numpy library we need to install it using pip install numpy
#Importing numpy library and giving it alias(nickname)-np import numpy as np list1=[100, 7127, 1812813, 27832728, 128718718, 812891, 2123, 133, 123, 1323231, 13131313] #Using mean function of numpy for calculating the average of #the list mean=np.mean(list1) print("average:",mean) Output- average: 15785572.727272727
Using statistics Library to Find the average of lists in python
#We can import the statistics library for finding the #average/mean of the list. import statistics de average 6.5 f average(list1): mean=statistics.mean(list1) return mean list1=[1,2,3,4,5,6,7,8,9,10,11,12] avg=average(list1) print("average",avg) Output- average 6.5
Using reduce function to find the average of lists in python
We can also find the mean or average of the list using the reduce() function of the functools library. In reduce function, we need to pass two parameters. The first one is the function that we want to perform on the two elements, and the second one is the list of items. Reduce function will automatically take two elements from the list. If we have a small function, it is a good practice to pass a lambda function as the first parameter.
Finding Average of list Without Python Lambda Function-
from functools import reduce def sum1(a,b): return (a+b) list1 = [12,345,21,54,11] print(reduce(sum1,list1)/len(list1)) Output- 88.6
Another way is by using the add function of the operator module –
import operator list1 = [12,345,21,54,11] print("average:", reduce (operator.add, list1)/len (list1))
Finding Average of list With Python Lambda Function-
from functools import reduce
sum1+=reduce(lambda a, b: a + b, list1)
list1 = [12,345,21,54,11]
Finding the average of the list in python containing strings
Suppose we have a list, containing numbers but in the form of strings, like this-
list_strings = [“1″,”2″,”3″,”4″,”5″,”6″,”7”]
We want to calculate the average of this string, but none of the methods we discussed can be used to calculate the average. We have to convert this list into integer type and then calculate the average of the list after any of the methods discussed.
import numpy as np def string_list_average(list_strings): for i in range(len(list_strings)): # Converting each element of list into int one by one list_strings[i]=int(list_strings[i]) return list_strings if name=="main": list_strings = ["1","2","3","4","5","6","7"] list1=string_list_average(list_strings) mean=np.mean(list1) print("average:",mean) Output- average: 4.0
A better way to convert the list of strings into an integer is by using the map function-
# Mapping each element of list as int
if name=="main": list_strings = ["11","21","13","41","51","16","17"] int_list=convert_string_to_int(list_strings) mean=statistics.mean(int_list) print("average:",mean) Output- average: 24.285714285714285
Note- We cannot find the average if the elements are like this – one, two, three, four.
Calculating The Average of Data Frame in python
import pandas as pd df=pd.DataFrame() list1=[1,2,3,4,5,6,7] list2=[11,21,31,141,151,161,17] df["column1"]=list1 df["column2"]=list2 print(df.mean()) Output- column1 4.000000 column2 76.142857 dtype: float64
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python in 2020
We have studied so many methods to find the average of the list in python. Python offers a variety of built-in functions like statistics.mean() and numpy.mean(). We can also find the average of a list containing numbers as a string.
Try to run the programs on your side and let us know if you have any queries.