I have a list of arrays, where each array is a list of lists. I want to turn this into a single array with all the columns. I've tried using for loops to get this done, but it feels like it should be doable in list comprehension. Is there a nice one-liner that will do this?
Example Input: [[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]]
Desired Output: [[1,2,7,8],[3,4,9,10],[5,6,11,12]]
Note: Example only has two arrays in the main list, but my actual data has much more, so I'm looking for something that works for N subarrays.
Edit: Example trying to solve this
Works for two but doesn't generalize:
[input[0][i]+input[1][i] for i in range(len(input[0]))]
These don't work, but show the idea:
[[element for table in input for element in row] for row in table]
[[*imput[j][i] for j in range(len(input))] for i in range(len(input[0]))]
Edit: Selected answer that uses only list comprehension and zip, but all answers (as of now) work, so use whichever fits your style/use case best.
You can generalize this from the standard list flattening pattern and zip:
>>> L = [[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]]
>>> list([y for z in x for y in z] for x in zip(*L))
[[1, 2, 7, 8], [3, 4, 9, 10], [5, 6, 11, 12]]
>>> L = [[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]],[[13,14],[15,16],[17,18]]]
>>> list([y for z in x for y in z] for x in zip(*L))
[[1, 2, 7, 8, 13, 14], [3, 4, 9, 10, 15, 16], [5, 6, 11, 12, 17, 18]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With