Open In App

Python | Average of each n-length consecutive segment in a list

Last Updated : 20 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list, the task is to find the average of each n-length consecutive segment where each segment contains n elements. Example: 

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
         12, 13, 14, 15, 16, 17, 18, 19, 20]

Output: [3, 4, 5, 6, 7, 8, 9, 10, 11, 
         12, 13, 14, 15, 16, 17, 18]

Explanation:
Segment 1 - [1, 2, 3, 4, 5] => 15/5 = 3
Segment 2 - [2, 3, 4, 5, 6] => 20/5 = 4
Segment 3 - [3, 4, 5, 6, 7] => 25/5 = 5
and so on..... 

  Method #1: Using list comprehension 

Python3




# Python code to find average of each consecutive segment
 
# List initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
         12, 13, 14, 15, 16, 17, 18, 19, 20]
 
# Defining Splits
splits = 5
 
# Finding average of each consecutive segment
Output = [sum(Input[i:i + splits])/splits
          for i in range(len(Input) - splits + 1)]
 
# printing output
print(Output)


Output:

[3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0]

Time Complexity: O(n), where n is the length of the list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

  Method #2: Using mean function 

Python3




# Python code to find average of each consecutive segment
 
# Importing
from statistics import mean
from itertools import islice
 
# List initialisation
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
         12, 13, 14, 15, 16, 17, 18, 19, 20]
 
# Finding average of each consecutive segment
zip_list = zip(*(islice(Input, i, None) for i in range(5)))
Output = list(map(mean, zip_list))
 
# printing output
print(Output)


Output:

[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

Method 3: using for loop

the for loop iterates over the range of len(Input) – splits + 1 to obtain each consecutive segment. It then calculates the average of the current segment and appends it to the Output list. Finally, it prints the Output list.

Step-by-step approach:

Initialize the Input list with the given values.
Set the splits variable to the desired length of each consecutive segment.
Create an empty list called Output.
Iterate over the range of len(Input) – splits + 1. This generates the starting indices of each consecutive segment in Input.
For each starting index i, obtain the corresponding segment of length splits from Input using slicing.
Calculate the average of the current segment using the sum() function and dividing by splits.
Append the current average to the Output list.
Once all segments have been processed, print the Output list.

Python3




# Python code to find average of each consecutive segment
 
# List initialization
Input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
         12, 13, 14, 15, 16, 17, 18, 19, 20]
 
# Defining Splits
splits = 5
 
# Finding average of each consecutive segment using for loop
Output = []
for i in range(len(Input) - splits + 1):
    segment = Input[i:i+splits]
    average = sum(segment) / splits
    Output.append(average)
 
# Printing output
print(Output)


Output

[3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0]

Time complexity: O(n) where n is the length of the input list Input, since we are iterating over each element of the list once. 
Auxiliary space: O(k), where k is the number of segments in the output. This is because we are storing the average of each segment in the Output list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads