Skip to content
Related Articles

Related Articles

Python | Convert Triple nesting to Double nesting list

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Aug, 2019
View Discussion
Improve Article
Save Article

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

Output :

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

Output :

The original list is : [[[1, 4, 6]], [[8, 9, 10, 7]]]
Double nested list from triple nested : [[1, 4, 6], [8, 9, 10, 7]]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!