Python | Convert Triple nesting to Double nesting list
Sometimes, while working with lists, we can have a problem in which we need to perform the flattening of nested list. This kind of problem have been discussed many times. But sometimes, flattening can be from a triple to double nesting as well. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension
This task can be performed using technique of list comprehension. In this, one can just take the initial element of triple nested list and just unpack it to double nested list.
# Python3 code to demonstrate working of # Convert Triple nesting to Double nesting list # using list comprehension # initialize list test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) # Convert Triple nesting to Double nesting list # using list comprehension res = [sub[0] for sub in test_list] # printing result print("Double nested list from triple nested : " + str(res)) |
The original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Method #2 : Using chain.from_iterable()
This task can also be performed using this function. This is the inbuilt method that is made to perform the task of flattening a list and hence is highly recommended to perform this task.
# Python3 code to demonstrate working of # Convert Triple nesting to Double nesting list # using chain.from_iterable() from itertools import chain # initialize list test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]] # printing original list print("The original list is : " + str(test_list)) # Convert Triple nesting to Double nesting list # using list comprehension res = list(chain.from_iterable(test_list)) # printing result print("Double nested list from triple nested : " + str(res)) |
The original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]] Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]
Recommended Posts:
- Python | Triple list difference
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of strings
- Python | Convert string enclosed list to list
- Python | Convert a nested list into a flat list
- Python | Convert list of strings to list of tuples
- Python | Convert a string representation of list into list
- Python | Convert list to indexed tuple list
- Python | Convert 1D list to 2D list of variable length
- Python | Convert list of tuples into list
- Python | Convert given list into nested list
- Python | Ways to Convert a 3D list into a 2D list
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.



