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 code to demonstrate working of # Convert 2D list to 3D at K slicing# Using loop # initializing listtest_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]] # printing original listprint("The original list is : " + str(test_list)) # initializing K K = 2 # Convert 2D list to 3D at K slicing# Using loopres = []subl = []cnt = 0for 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)) |
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.
# Python3 code to demonstrate working of # Convert 2D list to 3D at K slicing# Using zip() + list comprehension # initializing listtest_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]] # printing original listprint("The original list is : " + str(test_list)) # initializing K K = 2 # Convert 2D list to 3D at K slicing# Using zip() + list comprehensiontest_list = iter(test_list)temp = [test_list] * Kres = [list(ele) for ele in zip(*temp)] # printing result print("Records after conversion : " + str(res)) |
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]]]





Please Login to comment...