Python | Average of two lists
Last Updated :
11 May, 2023
The problem of finding a average values in a list is quite common. But sometimes this problem can be extended in two lists and hence becomes a modified problem. This article discusses shorthands by which this task can be performed easily. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using sum() + len() + “+” operator
The average value can be determined by the conventional sum() and len function of python and the extension of one to two lists can be dealt using the “+” operator.
Python3
# Python3 code to demonstrate
# Average of two lists
# using sum() + len() + "+" operator
# initializing lists
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
# printing the original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
# Average of two lists
# using sum() + len() + "+" operator
res = sum(test_list1 + test_list2) / len(test_list1 + test_list2)
# printing result
print ("The Average of both lists is : " + str(res))
OutputThe original list 1 is : [1, 3, 4, 5, 2, 6]
The original list 2 is : [3, 4, 8, 3, 10, 1]
The Average of both lists is : 4.166666666666667
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using sum() + len() + chain()
Another method to perform this particular task is by using the chain function which performs the task similar to the “+” operator but using an iterator, hence faster.
Python3
# Python3 code to demonstrate
# Average of two lists
# using sum() + len() + "+" operator
from itertools import chain
# initializing lists
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
# printing the original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
# Average of two lists
# using sum() + len() + "+" operator
res = sum(chain(test_list1, test_list2)) / len(list(chain(test_list1, test_list2)))
# printing result
print ("The Average of both lists is : " + str(res))
Output : The original list 1 is : [1, 3, 4, 5, 2, 6]
The original list 2 is : [3, 4, 8, 3, 10, 1]
The Average of both lists is : 4.166666666666667
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using extend() and mean() method of statistics module
extend() can be used to append a list into the other list and then statistics.mean() can be used to find the arithmetic mean of the new list that contains the elements from both the lists.
Python3
# Python3 code to demonstrate
# Average of two lists
import statistics
# initializing lists
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
# printing the original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Average of two lists
x = []
x.extend(test_list1)
x.extend(test_list2)
res = statistics.mean(x)
# printing result
print("The Average of both lists is : " + str(res))
OutputThe original list 1 is : [1, 3, 4, 5, 2, 6]
The original list 2 is : [3, 4, 8, 3, 10, 1]
The Average of both lists is : 4.166666666666667
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Approach 4: Using zip() and map()
Using zip() to combine the elements of the two lists
Using map() to add the elements at the same index of both lists and divide by 2
Using sum() to find the sum of the resulting list
Using len() to find the length of the resulting list
Python3
#Using zip() and map()
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
#Using zip() to combine the elements of the two lists
#Using map() to add the elements at the same index of both lists and divide by 2
#Using sum() to find the sum of the resulting list
#Using len() to find the length of the resulting list
res = sum(map(lambda x: (x[0] + x[1])/2, zip(test_list1, test_list2))) / len(test_list1)
#printing result
print("The Average of both lists is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe Average of both lists is : 4.166666666666667
Time Complexity: O(n) where n is the length of the lists
Auxiliary Space : O(n)
Method #5: Using Functions, for loop
Python3
# function to calculate the average of two lists
def average_lists(list1, list2):
# check if the lengths of the lists are equal
if len(list1) != len(list2):
return None
# calculate the sum of the elements of both lists
sum = 0
for i in range(len(list1)):
sum += list1[i] + list2[i]
# calculate the average and return it
return sum / (2*len(list1))
# example usage
list1 = [1, 3, 4, 5, 2, 6]
list2 = [3, 4, 8, 3, 10, 1]
average = average_lists(list1, list2)
if average is not None:
print("The Average of both lists is :", average)
else:
print("Lists are not of equal length")
OutputThe Average of both lists is : 4.166666666666667
Time Complexity: O(n) where n is the length of the lists
Auxiliary Space : O(n)
Similar Reads
Add Elements of Two Lists in Python Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Python - Dividing two lists Dividing two lists in Python means performing division between elements at the same position in both lists. Each element in the first list is divided by the corresponding element in the second list to create a new list of results. In this article, we will explore How to Divide two lists. Using NumPy
3 min read
Python | Merge two lists alternatively Given two lists, write a Python program to merge the given lists in an alternative fashion, provided that the two lists are of equal length. Examples: Input : lst1 = [1, 2, 3] lst2 = ['a', 'b', 'c'] Output : [1, 'a', 2, 'b', 3, 'c'] Input : lst1 = ['name', 'alice', 'bob'] lst2 = ['marks', 87, 56] Ou
4 min read
Average of Float Numbers - Python The task of calculating the average of float numbers in Python involves summing all the numbers in a given list and dividing the total by the number of elements in the list. For example, given a list of float numbers a = [6.1, 7.2, 3.3, 9.4, 10.6, 15.7], the goal is to compute the sum of the numbers
2 min read
Find Sum and Average of List in Python Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.Using
2 min read