Python | Mean of consecutive Sublist

Some of the classical problems in programming domain comes from different categories and one among them is finding the mean of subsets. This particular problem is also common when we need to compute the average and store consecutive group mean. Let’s try different approaches to this problem in python language.

Method #1 : Using list comprehension + sum()
The list comprehension can be used to perform the this particular task to filter out successive groups and sum function can be used to get the summation of the filtered solution. We divide the sum by sublist size for average.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Mean of consecutive Sublist
# using list comprehension + sum()
  
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14]
  
# printing original list 
print("The original list : " + str(test_list))
  
# using list comprehension + sum()
# Mean of consecutive Sublist
res = [ sum(test_list[x : x + 3]) / 3 for x in range(0, len(test_list), 3)]
  
# printing result
print("The grouped average list is : " + str(res))

chevron_right


Output :

The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The grouped average list is : [6.333333333333333, 12.333333333333334, 14.666666666666666]

 

Method #2 : Using sum() + itertools.islice()
The task of slicing the list into chunks is done by islice method here and the conventional task of getting the summation is done by the sum function as the above method. We divide the sum by sublist size for average.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Mean of consecutive Sublist
# using itertools.islice() + sum()
import itertools
  
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14]
  
# printing original list 
print("The original list : " + str(test_list))
  
# using itertools.islice() + sum()
# Mean of consecutive Sublist 
res = [sum(list(itertools.islice(test_list, i, i + 3))) / 3 for i in range(0, len(test_list), 3)]
  
# printing result
print("The grouped average list is : " + str(res))

chevron_right


Output :

The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The grouped average list is : [6.333333333333333, 12.333333333333334, 14.666666666666666]



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.