Change language

# Python | Ways to Convert 3D List to 2D List

|

List — it is a common type of data structure in Python. While we have used list and 2D list, the use of 3D list is increasing day by day, mainly in the case of web development.
Given the 3D list, the challenge is to convert it to a 2D list. Problems of this type are encountered when working on projects or when contributing to open source.
Here 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 to a 2D list.

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

Exit:

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

Method # 2: Using a comprehension list to convert a 3D list to a 2D list

 ` # Python code to convert a 3D list to a 2D list ` `   # Initialize the input list `` Input = [[[ 1 , 1 ], [ 2 , 7 ]], [[ 3 ], [ 4 ]], [[ 6 , 5 ], [ 6 ]]]   # Using the comprehension list Output = [elem for twod in Input for elem in twod]   # printout print ( "Initial 3d list is" ) print ( Input ) print ( " Converted 2d list is " ) print (Output) `

Exit:

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

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries