Python - Convert List to custom overlapping nested list
Last Updated :
05 Mar, 2023
Given a list, the task is to write a Python program to convert it into a custom overlapping nested list based on element size and overlap step.
Examples:
Input: test_list = [3, 5, 6, 7, 3, 9, 1, 10], step, size = 2, 4
Output: [[3, 5, 6, 7], [6, 7, 3, 9], [3, 9, 1, 10], [1, 10]]
Explanation: Rows sliced for size 4, and overcoming started after 2 elements of current row.
Input: test_list = [3, 5, 6, 7, 3, 9, 1, 10], step, size = 2, 3
Output: [[3, 5, 6], [6, 7, 3], [3, 9, 1], [1, 10]]
Explanation: Rows sliced for size 3, and overcoming started after 2 elements of current row.
Method #1: Using list slicing + loop
In this, row size is managed by slicing operation and overlap step is managed by step mentioned in range() while using a loop for iteration.
Python3
# Python3 code to demonstrate working of
# Convert List to custom overlapping Matrix
# Using list slicing + loop
# initializing list
test_list = [3, 5, 6, 7, 3, 9, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# initializing step, size
step, size = 2, 4
res = []
for idx in range(0, len(test_list), step):
# slicing list
res.append(test_list[idx: idx + size])
# printing result
print("The created Matrix : " + str(res))
Output:
The original list is : [3, 5, 6, 7, 3, 9, 1, 10]
The created Matrix : [[3, 5, 6, 7], [6, 7, 3, 9], [3, 9, 1, 10], [1, 10]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
In this, similar functionality as the above method is used with a variation of having shorthand using list comprehension.
Python3
# Python3 code to demonstrate working of
# Convert List to custom overlapping Matrix
# Using list comprehension
# initializing list
test_list = [3, 5, 6, 7, 3, 9, 1, 10]
# printing original list
print("The original list is : " + str(test_list))
# initializing step, size
step, size = 2, 4
# list comprehension used as shorthand to solve problem
res = [test_list[idx: idx + size] for idx in range(0,
len(test_list),
step)]
# printing result
print("The created Matrix : " + str(res))
Output:
The original list is : [3, 5, 6, 7, 3, 9, 1, 10]
The created Matrix : [[3, 5, 6, 7], [6, 7, 3, 9], [3, 9, 1, 10], [1, 10]]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(m)
Similar Reads
Convert Nested Dictionary to List in Python In this article, weâll explore several methods to Convert Nested Dictionaries to a List in Python. List comprehension is the fastest and most concise way to convert a nested dictionary into a list.Pythona = { "a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}, } # Convert nested dictionary to a list of li
3 min read
Python | Convert given list into nested list Sometimes, we come across data that is in string format in a list and it is required to convert it into a list of the list. This kind of problem of converting a list of strings to a nested list is quite common in web development. Let's discuss certain ways in which this can be performed. Convert the
4 min read
Convert List of Lists to Dictionary - Python We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite
3 min read
Python | Convert a nested list into a flat list In this article, we will cover how to Flatten a List of Lists in python. To convert a nested list into a flat list we are going to see some examples. Example: Input : l = [1, 2, [3, 4, [5, 6] ], 7, 8, [9, [10] ] ] Output : l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Input : l = [[['item1', 'item2']], [['ite
5 min read
Python - Convert List of Dictionaries to List of Lists We are given list of dictionaries we need to convert it to list of lists. For example we are given a list of dictionaries a = [{'name': 'Geeks', 'age': 25}, {'name': 'Geeks', 'age': 30}] we need to convert it in list of list so that the output becomes[['Geeks',25],['Geeks;'30]].Using List Comprehens
3 min read