Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Convert 2D list to 3D at K slicing

Python – Convert 2D list to 3D at K slicing

Sometimes, while working with Python lists, we can have a problem in which we need to convert a 2D list to 3D, at every Kth list. This type of problem is peculiar, but can have application in various data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]] , K = 3 
Output : [[[6, 5], [2, 3], [3, 1]], [[4, 6], [3, 2], [1, 6]]] 

Input : test_list = [[6, 5], [2, 3], [3, 1]] , K = 1 
Output : [[[6, 5]], [[2, 3]], [[3, 1]]]

Method #1: Using loop This is brute way in which this task can be performed. In this, we iterate through each element, and maintain a counter, at every Kth sublist, create a new list and append accordingly. 

Python3




# Python3 code to demonstrate working of
# Convert 2D list to 3D at K slicing
# Using loop
 
# initializing list
test_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# Convert 2D list to 3D at K slicing
# Using loop
res = []
subl = []
cnt = 0
for sub in test_list:
    subl.append(sub)
    cnt = cnt + 1
    if cnt >= K:
        res.append(subl)
        subl = []
        cnt = 0
 
# printing result
print("Records after conversion : " + str(res))


Output : 

The original list is : [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
Records after conversion : [[[6, 5], [2, 3]], [[3, 1], [4, 6]], [[3, 2], [1, 6]]]

Time complexity: O(n), where n is the length of the input list. The program uses a loop to iterate through the input list once.
Auxiliary Space: O(k), where k is the value of K. The program creates a new list of sublists for each group of K elements in the input list. The size of each sublist is at most K, so the total auxiliary space used by the program is O(k).

Method #2 : Using zip() + list comprehension The combination of above functions can also be used to solve this problem. In this, we perform task by first chunking the values according to size and then list comprehension along with zip() is used for dimension conversion. 

Python3




# Python3 code to demonstrate working of
# Convert 2D list to 3D at K slicing
# Using zip() + list comprehension
 
# initializing list
test_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# Convert 2D list to 3D at K slicing
# Using zip() + list comprehension
test_list = iter(test_list)
temp = [test_list] * K
res = [list(ele) for ele in zip(*temp)]
 
# printing result
print("Records after conversion : " + str(res))


Output : 

The original list is : [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
Records after conversion : [[[6, 5], [2, 3]], [[3, 1], [4, 6]], [[3, 2], [1, 6]]]

Time complexity: O(n), where n is the number of elements in the 2D list.
Auxiliary space: O(n/K), where K is the slicing index. The program creates a temporary list of length K,

Method #3: Using itertools.islice()

The itertools module provides the islice() function, which can be used to slice an iterable object like a list or a tuple. We can use islice() to split the 2D list into chunks of size K and then convert each chunk to a sublist of a new 3D list.

Python3




import itertools
 
# initializing list
test_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
 
# initializing K
K = 2
 
# Convert 2D list to 3D at K slicing
# Using itertools.islice()
it = iter(test_list)
res = [list(itertools.islice(it, K)) for _ in range(len(test_list)//K)]
 
# printing result
print("Records after conversion : " + str(res))


Output

Records after conversion : [[[6, 5], [2, 3]], [[3, 1], [4, 6]], [[3, 2], [1, 6]]]

Time complexity: O(N/K * K), which simplifies to O(N), where N is the number of elements in the 2D list.
Auxiliary space: O(K), which is the size of each sublist that we create using islice(). 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments