Python | Average of two lists

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.