Reputation: 588
So I have a list of lists in python that is something like this:
[[[0, 1, 0, 1, 0]]
[[1, 1, 1, 1, 1]]
[[1, 0, 0, 1, 1]]
[[0, 1, 0, 0, 0]]]
I want to flatten this list and end up with this:
[[0, 1, 0, 1, 0]
[1, 1, 1, 1, 1]
[1, 0, 0, 1, 1]
[0, 1, 0, 0, 0]]
Is there a straightforward way to do this in python?
Upvotes: 0
Views: 633
Reputation: 27869
Using numpy.squeeze you can do what you want:
import numpy as np
a = np.array([[[0, 1, 0, 1, 0]],
[[1, 1, 1, 1, 1]],
[[1, 0, 0, 1, 1]],
[[0, 1, 0, 0, 0]]])
a.squeeze()
[[0 1 0 1 0]
[1 1 1 1 1]
[1 0 0 1 1]
[0 1 0 0 0]]
Upvotes: 2
Reputation: 1924
a = [[[0, 1, 0, 1, 0]],
[[1, 1, 1, 1, 1]],
[[1, 0, 0, 1, 1]],
[[0, 1, 0, 0, 0]]]
[i[0] for i in a]
output
[[0, 1, 0, 1, 0], [1, 1, 1, 1, 1], [1, 0, 0, 1, 1], [0, 1, 0, 0, 0]]
Upvotes: 2