Related Articles

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

• Last Updated : 03 Jul, 2020

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

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up