
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert 3D List into 2D List in Python
Python is a widely used programming language used for different purposes such as Web Development, Data Science, Machine Learning and to perform different operations with automations. In this article we will learn about different ways to convert a 3D list into 2D list.
Let's first have a look how does both the types of list look like: -
3D List
Three_dimensional_list = [ [['Sam', 'Tom'], ['Miller', 'David']], [['Rocky', 'Tyler'], ['Mason', 'Petrick']], [['Stefen', 'Damon'], ['Matt', 'Jeremy']] ] # This is the basic structure of a three dimensional list
2D List
Two_dimensional_list = [ ['Sam', 'Tom'], ['Miller', 'David'], ['Rocky', 'Tyler'], ['Mason', 'Petrick'], ['Stefen', 'Damon'], ['Matt', 'Jeremy'] ] # This is the basic structure of a two Dimensional list
Different Methods to convert 3D List into a 2D List
Nested Loop
This is the fastest method to convert a 3D list into a 2D list. We will simply check each element in the list and flatten it into a 2D list. Let's have a look at the example to understand it in a better way: -
Example
def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D final_list = [] # An empty list is created to store all the 2 dimension list elements for sublist in main_list: # For loop is used to check each element in the sublists in the 3D list for subsublist in sublist: # Second for loop to chek each element in the sublists of sublists of the 3D list final_list.append(subsublist) # All the sublists are moved to the final new list created return final_list Three_dimensional_list = [ [['Sam', 'Tom'], ['Miller', 'David']], [['Rocky', 'Tyler'], ['Mason', 'Petrick']], [['Stefen', 'Damon'], ['Matt', 'Jeremy']] ] # The 3D list is given as input Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run print(Two_dimensional_list)
Output
The output of the above example will be as follows: -
[['Sam', 'Tom'], ['Miller', 'David'], ['Rocky', 'Tyler'], ['Mason', 'Petrick'], ['Stefen', 'Damon'], ['Matt', 'Jeremy']] # The 3D list is converted into 2D list
List Comprehension
This is a very efficient way to check each element present in the list given as input. Let's have a look at the example to understand it in a better manner: -
Example
def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D return [subsublist for sublist in main_list for subsublist in sublist] # We will use for loop to check each element in sublist and sublist of sublist and convert them into a 2D list Three_dimensional_list = [ [['Sam', 'Tom'], ['Miller', 'David']], [['Rocky', 'Tyler'], ['Mason', 'Petrick']], [['Stefen', 'Damon'], ['Matt', 'Jeremy']] ] # The 3D list is given as input Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run print(Two_dimensional_list) # Note: This method is more preferable because it is a compact method and less lines of codes are required
Output
The output of the above example will be as follows: -
[['Sam', 'Tom'], ['Miller', 'David'], ['Rocky', 'Tyler'], ['Mason', 'Petrick'], ['Stefen', 'Damon'], ['Matt', 'Jeremy']] # The 3D list is converted into 2D list
Numpy Library
This method is mostly useful in the case when the input data in the list is in numerical form. Let's have a look at an example to understand it in a better manner: -
Example
import numpy as np # Do not forget to import numpy or else error might occur def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D flattened = np.array(main_list).reshape(-1, len(main_list[0][0])) # With the help of np.array we will convert the input list into an array and with the help of reshape, we reshape the array into 2D form return flattened.tolist() # The 2D form of array will be converted back into list with the help of tolist function Three_dimensional_list = [ [[14, 28], [33, 47]], [[59, 36], [71, 18]], [[96, 13], [81, 32]] ] # The list is given as input Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run print(Two_dimensional_list)
Output
The output of the above example will be as follows: -
[[14, 28], [33, 47], [59, 36], [71, 18], [96, 13], [81, 32]] # The 3D list is converted into 2D list
Manual Iteration
In this method we will manually check each element in the list given as input. Let's have a look at an example to understand it in a better manner: -
Example
def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D # We will first calculate the dimensions of the 2D List rows = len(main_list) * len(main_list[0]) # # The number of rows in 2D list will be decide by product of number of sublist in 3D list with the number of sublists in each sublists cols = len(main_list[0][0]) output_list = [[0 for _ in range(cols)] for _ in range(rows)] # A new list to store elements is created and for loop is used to check each element in the list given as input for i in range(len(main_list)): for j in range(len(main_list[i])): for k in range(len(main_list[i][j])): output_list[i * len(main_list[i]) + j][k] = main_list[i][j][k] # Proper values are assigned different positions in the new list created return output_list Three_dimensional_list = [ [['Sam', 'Tom'], ['Miller', 'David']], [['Rocky', 'Tyler'], ['Mason', 'Petrick']], [['Stefen', 'Damon'], ['Matt', 'Jeremy']] ] # The 3D list is given as input Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run print(Two_dimensional_list)
Output
The output of the above example will be as follows: -
[['Sam', 'Tom'], ['Miller', 'David'], ['Rocky', 'Tyler'], ['Mason', 'Petrick'], ['Stefen', 'Damon'], ['Matt', 'Jeremy']] # The 3D list is converted into 2D list
Conclusion
It is important to have knowledge about different methods that can be used to convert 3D list into 2D list to become an efficient programmer. One can select any of the above mentioned method as per convenience and depending upon the field of application of the program.