
- 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
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.
Example
Below is a demonstration of the same
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)
Output
The list is : [31, 25, 36, 76, 73, 89, 91, 100] The result is : [[31, 25, 36, 76], [76, 73, 89, 91], [91, 100]]
Explanation
A list is defined and is displayed on the console.
Two integers are defined.
An empty list is defined.
The original list is iterated over, and the element at a specific index is appended to the empty list.
This list is the result which is displayed as output on the console.
- Related Articles
- Python - Convert given list into nested list
- Convert a nested list into a flat list in Python
- Python - Convert list of nested dictionary into Pandas Dataframe
- Python program to Flatten Nested List to Tuple List
- Convert Nested Tuple to Custom Key Dictionary in Python
- Nested list comprehension in python
- Python - Convert list of string to list of list
- Custom list split in Python
- Python How to copy a nested list
- Convert list of string to list of list in Python
- Convert list of tuples to list of list in Python
- Convert string enclosed list to list in Python
- Flatten Nested List Iterator in Python
- Python – Custom Lower bound a List
- Python - Convert List of lists to List of Sets

Advertisements