Average of two lists Python
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. Show
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. 3+4+5+6+7=25 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. Contents
Finding Average of lists in python using Looplist1=[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.5We can also use the sum() method directly- list1=[1,2,3,4,5,6]
Avg=sum(list1)/len(list1)
print(Avg)
Output-
3.5Finding the average of lists in Python using the Numpy LibraryFor 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.727272727Using 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.5Using reduce function to find the average of lists in PythonWe 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 Functionfrom functools import reduce
def sum1(a,b):
return (a+b)
list1 = [12,345,21,54,11]
print(reduce(sum1,list1)/len(list1))
Output-
88.6Another way is by using the add function of the operator moduleimport operator
list1 = [12,345,21,54,11]
print("average:", reduce (operator.add, list1)/len (list1))
Finding Average of the list With Python Lambda Functionfrom functools import reduce
def average(list1):
sum1=0
sum1+=reduce(lambda a, b: a + b, list1)
avg=sum1/ len(list1)
return avg
list1 = [12,345,21,54,11]
print("average",average(list1) )
Output-
average: 88.6Finding the average of the list in python containing stringsSuppose we have a list, containing numbers but in the form of strings, like this- 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.0A better way to convert the list of strings into an integer is by using the map function- import statistics
def convert_string_to_int(list_strings):
# Mapping each element of list as int
int_list=list(map(int,list_strings))
return int_list
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.285714285714285Note- We cannot find the average if the elements are like this one, two, three, four. Calculating The Average of Data Frame in Pythonimport 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: float64Must Read
ConclusionWe 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. Happy Coding! |