Open In App

Convert 1D list to 2D list of variable length- Python

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.



Next Article
Practice Tags :

Similar Reads