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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]]]

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]]]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.