Open In App

Python | Convert Triple nesting to Double nesting list

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with lists, we can have a problem in which we need to perform the flattening of a nested list. This kind of problem has 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 the technique of list comprehension. In this, one can just take the initial element of the triple nested list and just unpack it to a double nested list. 

Python3




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

Time complexity: O(n), where n is the total number of elements in the list.
Auxiliary space: O(n), where n is the total number of elements in the list.

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




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

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #3 : Using extend() method

Python3




# Python3 code to demonstrate working of
# Convert Triple nesting to Double nesting list
 
# 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
res = []
for i in test_list:
    x = []
    for j in i:
        x.extend(j)
    res.append(x)
# 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 #4: Using numpy.array

Note: Install numpy using “pip install numpy”

The numpy library can be used to convert the triple nested list to a double nested list. Time complexity of this method is O(n) and space complexity is O(n) as well, since we are using numpy to convert the nested list to a flat array.

Python3




# Python3 code to demonstrate working of
# Convert Triple nesting to Double nesting list
# using numpy
 
# importing numpy
import numpy as np
 
# 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 numpy
res = np.array(test_list).ravel().tolist()
 
# printing result
print("Double nested list from triple nested : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


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

Time complexity: O(n), where n is the total number of elements in the input list.

Auxiliary space: O(n), where n is the total number of elements in the input list. 

Method #5 : Using sum() and [] operator

Python3




test_list = [[[1, 4, 6]], [[8, 9, 10, 7]]]
# printing original list
print("The original list is : " + str(test_list))
res = sum(test_list, [])
# printing result
print("Double nested list from triple nested : " + str(res))
#This code is contributed by Jyothi pinjala


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

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #6: Using map() function and lambda expression

Approach:

  1. Initialize the list test_list with the given values.
  2. Print the original list using the print() function and str() function to convert it to a string.
  3. Use the map() function to apply a lambda expression to each element in the list. The lambda expression takes an element x and returns the first sub-element of x, i.e., x[0].
  4. Convert the result to a list using the list() function.
  5. Print the result using the print() function and str() function to convert it to a string.

Python3




# Python3 code to demonstrate working of
# Convert Triple nesting to Double nesting list
# using map() and lambda expression
 
# 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 map() and lambda expression
res = list(map(lambda x: x[0], 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]]

Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), where n is the number of elements in the list (for storing the result list).



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads