Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Convert List to custom overlapping nested list
When it is required to convert a list to a customized overlapping nested list, an iteration along with the 'append' method can be used. This technique creates sublists of a specified size with a defined step interval, allowing elements to overlap between consecutive sublists.
Syntax
for index in range(0, len(list), step):
result.append(list[index: index + size])
Parameters
The overlapping nested list conversion uses two key parameters:
- step ? The interval between starting positions of consecutive sublists
- size ? The length of each sublist
Example
Below is a demonstration of converting a list to overlapping nested sublists ?
my_list = [31, 25, 36, 76, 73, 89, 91, 100]
print("The list is :")
print(my_list)
my_step, my_size = 3, 4
my_result = []
for index in range(0, len(my_list), my_step):
my_result.append(my_list[index: index + my_size])
print("The result is :")
print(my_result)
The output of the above code is ?
The list is : [31, 25, 36, 76, 73, 89, 91, 100] The result is : [[31, 25, 36, 76], [76, 73, 89, 91], [91, 100]]
How It Works
The algorithm works by iterating through the original list with a specified step size and creating sublists of a defined length:
- First iteration ? Starts at index 0, takes elements [31, 25, 36, 76]
- Second iteration ? Starts at index 3, takes elements [76, 73, 89, 91]
- Third iteration ? Starts at index 6, takes remaining elements [91, 100]
Notice how element 76 appears in both first and second sublists, and element 91 appears in both second and third sublists, creating the overlapping effect.
Using List Comprehension
The same result can be achieved more concisely using list comprehension ?
my_list = [31, 25, 36, 76, 73, 89, 91, 100]
my_step, my_size = 3, 4
my_result = [my_list[i:i + my_size] for i in range(0, len(my_list), my_step)]
print("The result is :")
print(my_result)
The output of the above code is ?
The result is : [[31, 25, 36, 76], [76, 73, 89, 91], [91, 100]]
Different Step and Size Combinations
Here's how different step and size values affect the overlapping pattern ?
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Case 1: step=2, size=3 (more overlap)
result1 = [data[i:i + 3] for i in range(0, len(data), 2)]
print("Step=2, Size=3:", result1)
# Case 2: step=1, size=3 (maximum overlap)
result2 = [data[i:i + 3] for i in range(0, len(data), 1)]
print("Step=1, Size=3:", result2[:5]) # showing first 5 for brevity
The output of the above code is ?
Step=2, Size=3: [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]] Step=1, Size=3: [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
Conclusion
Converting a list to overlapping nested sublists is useful for sliding window operations and data analysis. Use smaller step sizes for more overlap or larger step sizes for less overlap between consecutive sublists.
