
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Average of each n-length consecutive segment in a Python list
We have a list containing only numbers. We plan to get the average of a set of sequential numbers from the list which keeps rolling from the first number to next number and then to next number and so on.
Example
The below example simplifies the requirement of finding the average of each 4-length consecutive elements of the list.
Given list: [10,12,14,16,18,20,22,24,26] Average of every segment of 4 consecutive numbers: [13.0, 15.0, 17.0, 19.0, 21.0, 23.0]
With sum and range
We use the list comprehension approach to take the sum of the consecutive numbers by applying range to keep track of how many numbers we gathered. Then we keep dividing the sum with the segment length with the help of a loop. Finally the result is gathered into a new list.
Example
listA = [10,12,14,16,18,20,22,24,26] print("Given list:\n",listA) seg = 4 # List comprehension res = [sum(listA[m:m + seg])/seg for m in range(len(listA) - seg + 1)] print("new list with averages:\n ",res)
Running the above code gives us the following result −
Given list: [10, 12, 14, 16, 18, 20, 22, 24, 26] new list with averages: [13.0, 15.0, 17.0, 19.0, 21.0, 23.0]
With islice and mean
In this approach we take help of python modules which can calculate these values in a more direct way. We keep slicing the elements of the list in the given range using the isslice function and then apply the mean function directly on the new list to get the final result.
Example
from statistics import mean from itertools import islice listA = [10,12,14,16,18,20,22,24,26] print("Given list:\n",listA) # With islice and mean listB = zip(*(islice(listA, i, None) for i in range(4))) res = list(map(mean, listB)) print("new list with averages:\n ",res)
Output
Running the above code gives us the following result −
Given list: [10, 12, 14, 16, 18, 20, 22, 24, 26] new list with averages: [13, 15, 17, 19, 21, 23]
- Related Articles
- Find average of a list in python?
- Python – Consecutive Division in List
- Python – Average digits count in a List
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Length of a Linked List in Python
- Consecutive elements pairing in list in Python
- Draw a line segment of length $8.6\ cm$. Bisect it and measure the length of each part.
- Program to find length of longest consecutive sequence in Python
- Maximum Average Subtree in Python\n
- Finding average of n top marks of each student in JavaScript
- Program to find length of longest consecutive path of a binary tree in python
- Python - Consecutive Ranges of K greater than N
- Check if list contains consecutive numbers in Python
- Program to find sum of concatenated pairs of all each element in a list in Python?\n
- Find maximum length sub-list in a nested list in Python
