Convert 1D list to 2D list of variable length- Python
Last Updated :
01 Feb, 2025
The task of converting a 1D list to a 2D list of variable length in Python involves dynamically dividing a single list into multiple sublists, where each sublist has a different number of elements based on a specified set of lengths.
For example, given a list [1, 2, 3, 4, 5, 6] and length specifications [1, 2, 3], the goal is to create a 2D list like [[1], [2, 3], [4, 5, 6]] by partitioning the original list into sublists of sizes defined by the second list.
Using iterator
When converting a 1D list to a 2D list of variable lengths, islice() from itertools module offers an efficient way to slice the list into chunks without storing the entire list in memory. It allows for sequential processing, making it ideal for handling large datasets where memory efficiency is key. With islice() , we can process each chunk of data on the fly, which is perfect for scenarios that involve large-scale data manipulation.
Python
from itertools import islice
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3] # Defines the variable lengths of sublists
it = iter(a) # Converts the `a` into an iterator
res = [list(islice(it, size)) for size in b]
print(res)
Output[[1], [2, 3], [4, 5, 6]]
Explanation: list comprehension iterates over each value in b and uses islice(it, size) to slice the first size elements from the iterator it. islice() returns an iterator, so list() converts it into a list.
Using list slicing
List slicing is widely-used technique to split a 1D list into sublists of variable lengths. By specifying start and end indices, we can easily extract chunks of data based on the desired sublist sizes. This method is highly readable, making it ideal for beginners or those who prioritize code clarity. While it's great for moderate-sized datasets, it may not be the best option for very large lists due to memory consumption.
Python
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3] # Defines the variable lengths of sublists
idx = 0 # Initialize index to start
res = [] # Initialize an empty list
for size in b:
res.append(a[idx : idx + size])
idx += size
print(res)
Output[[1], [2, 3], [4, 5, 6]]
Explanation: for size in b iterates through each sublist size in b. For each iteration, a[idx : idx + size] slices a portion of a from idx to idx + size and appends it to res. Afterward, idx += size updates the index to point to the next section of the list for the next slice.
Using deque
If we need to dynamically partition a 1D list into smaller sublists, deque from the collections module allows for efficient removal of elements from the front. popleft() is optimized for constant-time removals, which makes it a great option for partitioning a list while modifying its contents. This approach is particularly useful for real-time data processing or scenarios where data chunks are being processed and removed dynamically.
Python
from collections import deque
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3] # Defines the variable lengths of sublists
dq = deque(a) # Convert list 'a' to a deque
res = [ [dq.popleft() for _ in range(size)] for size in b ]
print(res)
Output[[1], [2, 3], [4, 5, 6]]
Explanation:res = [ [dq.popleft() for _ in range(size)] for size in b ] creates sublists by iterating over each size in b. For each size, it pops that many elements from the front of the deque dq using popleft(), efficiently removing elements and forming sublists.
Using list comprehension
List comprehension offers a efficient way to convert a 1D list into multiple sublists. By using a loop with pop(0), we can extract elements from the list and group them into sublists of varying lengths in a single line of code.
Python
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3] # Defines the variable lengths of sublists
res = [ [a.pop(0) for _ in range(size)] for size in b ]
print(res)
Output[[1], [2, 3], [4, 5, 6]]
Explanation:res = [ [a.pop(0) for _ in range(size)] for size in b ] creates sublists by iterating over each size in b. For each size, it uses a.pop(0) to remove the first element of a and repeat this process size times, forming a sublist of the specified length.
Similar Reads
Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read
Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
3 min read
Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu
3 min read
Convert Value List Elements to List Records - Python We are given a dictionary with lists as values and the task is to transform each element in these lists into individual dictionary records. Specifically, each list element should become a key in a new dictionary with an empty list as its value. For example, given {'gfg': [4, 5], 'best': [8, 10, 7, 9
3 min read
Convert Set of Tuples to a List of Lists in Python Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list
3 min read