Subbu Dantu Subbu Dantu - 2 months ago 15
Python Question

How can I convert a 3D list into a 2D list in python?

This is the code that I have:

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


How can I change it into:

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


?

Answer Source

You want to flatten a single level of the input list, try this solution using a list comprehension:

lst = [[[3], [4]], [[5], [6]], [[7], [8]]]
[e for sl in lst for e in sl]
=> [[3], [4], [5], [6], [7], [8]]