Open In App

Python | Ways to Convert a 3D list into a 2D list

Improve
Improve
Like Article
Like
Save
Share
Report

List is a common type of data structure in Python. While we have used the list and 2d list, the use of 3d list is increasing day by day, mostly in case of web development. Given a 3D list, the task is to convert it into a 2D list. These type of problems are encountered while working on projects or while contributing to open source. Below are some ways to achieve the above task.

Input:
[[[3], [4]], [[5], [6]], [[7], [8]]]
Output:
[[3], [4], [5], [6], [7], [8]]

Method #1: Using simple iteration to convert a 3D list into a 2D list. 

Python3




# Python code to convert a 3D list into a 2D list
 
# Input list initialization
Input = [[[3], [4]], [[5], [6]], [[7], [8]]]
 
# Output list initialization
Output = []
 
# Using iteration
for temp in Input:
    for elem in temp:
        Output.append(elem)
 
# printing output
print("Initial 3d list is")
print(Input)
print("Converted 2d list is")
print(Output)


Output:

Initial 3d list is
[[[3], [4]], [[5], [6]], [[7], [8]]]
Converted 2d list is
[[3], [4], [5], [6], [7], [8]]

Method #2: Using List Comprehension to convert a 3D list into a 2D list 

Python3




# Python code to convert a 3D list into a 2D list
 
# Input list initialization
Input = [[[1, 1], [2, 7]], [[3], [4]], [[6, 5], [6]]]
 
# Using list comprehension
Output = [elem for twod in Input for elem in twod]
 
# printing output
print("Initial 3d list is")
print(Input)
print("Converted 2d list is")
print(Output)


Output:

Initial 3d list is
[[[1, 1], [2, 7]], [[3], [4]], [[6, 5], [6]]]
Converted 2d list is
[[1, 1], [2, 7], [3], [4], [6, 5], [6]]

Method #3: Using Reduce and Chain from itertools to convert a 3D list into a 2D list
 

Python3




# Python code to convert a 3D list into a 2D list
   
# importing itertools for chain
import itertools
   
# Input list initialization
Input = [[[1, 1], [2, 7]], [[3], [4]], [[6, 5], [6]]]
   
# Using reduce and chain
Output = list(itertools.chain.from_iterable(Input))
   
# printing output
print("Initial 3d list is")
print(Input)
print("Converted 2d list is")
print(Output)
#This code is contributed by Edula Vinay Kumar Reddy


Output

Initial 3d list is
[[[1, 1], [2, 7]], [[3], [4]], [[6, 5], [6]]]
Converted 2d list is
[[1, 1], [2, 7], [3], [4], [6, 5], [6]]

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

Method 4:Using recursion method

APPROACH:

The approach used in this program is to recursively traverse the given input list and append the elements to the output list. Whenever we encounter a nested list, we call the flatten_list function again to flatten it and append the elements to the output list.

ALGORITHM:

1. Define a function flatten_list(lst) that takes a nested list lst as input.
2. Initialize an empty list result to store the flattened list.
3. Loop through each element in lst.
4. If the element is a list, recursively call flatten_list function and append its elements to result.
5. If the element is not a list, append it to result.
6. Return the flattened list result

Python3




def flatten_list(lst):
    result = []
    for elem in lst:
        if isinstance(elem, list):
            result.extend(flatten_list(elem))
        else:
            result.append(elem)
    return result
 
input_list = [[[3], [4]], [[5], [6]], [[7], [8]]]
output_list = flatten_list(input_list)
print(output_list)


Output

[3, 4, 5, 6, 7, 8]

Time complexity of O(n) and space complexity of O(n), where n is the total number of elements in the 3D list.



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