Python – Mean of Consecutive Sublist

When working with lists in Python, calculating the mean of consecutive sublists is a common task in data analysis. This involves dividing a list into fixed-size chunks and computing the average of each chunk. Python offers several approaches to accomplish this efficiently.

What is Mean of Consecutive Sublists?

Given a list and a sublist size, we divide the original list into consecutive sublists of that size and calculate the mean (average) of each sublist. For example, if we have [1, 2, 3, 4, 5, 6] and sublist size is 3, we get sublists [1, 2, 3] and [4, 5, 6] with means 2.0 and 5.0 respectively.

Method 1: Using List Comprehension

List comprehension provides a concise way to create lists and calculate means in a single line ?

# Initialize the list and sublist size
input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sublist_size = 3

# Calculate means using list comprehension
result_list = [sum(input_list[i:i+sublist_size]) / sublist_size 
               for i in range(0, len(input_list), sublist_size)]

print("Original list:", input_list)
print("Sublist size:", sublist_size)
print("Means of consecutive sublists:", result_list)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Sublist size: 3
Means of consecutive sublists: [2.0, 5.0, 8.0]

How It Works

The list comprehension iterates through the list with steps equal to the sublist size. For each position i, it extracts a slice input_list[i:i+sublist_size], calculates the sum, and divides by the sublist size to get the mean.

Method 2: Using NumPy

NumPy provides efficient numerical operations and can handle array operations more efficiently for large datasets ?

import numpy as np

# Initialize the list and sublist size
input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sublist_size = 3

# Calculate means using NumPy
result_array = [np.mean(input_list[i:i+sublist_size]) 
                for i in range(0, len(input_list), sublist_size)]

print("Original list:", input_list)
print("Sublist size:", sublist_size)
print("Means using NumPy:", result_array)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Sublist size: 3
Means using NumPy: [2.0, 5.0, 8.0]

Method 3: Using a Simple Loop

A traditional loop approach provides more control and is easier to understand for beginners ?

# Initialize the list and sublist size
input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sublist_size = 3
result_list = []

# Calculate means using a loop
for i in range(0, len(input_list), sublist_size):
    sublist = input_list[i:i+sublist_size]
    mean_value = sum(sublist) / len(sublist)
    result_list.append(mean_value)

print("Original list:", input_list)
print("Sublist size:", sublist_size) 
print("Means using loop:", result_list)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Sublist size: 3
Means using loop: [2.0, 5.0, 8.0]

Comparison

Method Readability Performance Best For
List Comprehension Concise Good Small to medium datasets
NumPy Clean Excellent Large numerical datasets
Simple Loop Very clear Good Learning and debugging

Conclusion

All three methods effectively calculate the mean of consecutive sublists. Use list comprehension for concise code, NumPy for numerical performance, or simple loops for clarity and learning purposes.

Updated on: 2026-03-27T11:17:52+05:30

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements