Python | Convert Triple nesting to Double nesting list
Last Updated :
17 Apr, 2023
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
test_list = [[[ 1 , 4 , 6 ]], [[ 8 , 9 , 10 , 7 ]]]
print ( "The original list is : " + str (test_list))
res = [sub[ 0 ] for sub in test_list]
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
from itertools import chain
test_list = [[[ 1 , 4 , 6 ]], [[ 8 , 9 , 10 , 7 ]]]
print ( "The original list is : " + str (test_list))
res = list (chain.from_iterable(test_list))
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
test_list = [[[ 1 , 4 , 6 ]], [[ 8 , 9 , 10 , 7 ]]]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
x = []
for j in i:
x.extend(j)
res.append(x)
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
import numpy as np
test_list = [[[ 1 , 4 , 6 ]], [[ 8 , 9 , 10 , 7 ]]]
print ( "The original list is : " + str (test_list))
res = np.array(test_list).ravel().tolist()
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 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 ]]]
print ( "The original list is : " + str (test_list))
res = sum (test_list, [])
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)
Auxiliary Space: O(n)
Method #6: Using map() function and lambda expression
Approach:
- Initialize the list test_list with the given values.
- Print the original list using the print() function and str() function to convert it to a string.
- 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].
- Convert the result to a list using the list() function.
- Print the result using the print() function and str() function to convert it to a string.
Python3
test_list = [[[ 1 , 4 , 6 ]], [[ 8 , 9 , 10 , 7 ]]]
print ( "The original list is : " + str (test_list))
res = list ( map ( lambda x: x[ 0 ], test_list))
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).
Share your thoughts in the comments
Please Login to comment...