0

I have two arrays of arrays:

[array[(1, 2, 3)],
array[(4, 5, 6, 7)],
array[(8, 9, 9.5)]]

[array[(10, 11, 12)],
array[(13, 14, 15)],
array[(16, 17, 18, 19)]]

What I need to have is:

[[array([1, 2, 3]),
  array([10, 11, 12])],
 [array([4, 5, 6, 7]),
  array([13, 14, 15])],
 [array([7, 8, 9.5]),
  array([16, 17, 18, 19])]]

I can do by looping into the two arrays but it not really productif, any idea guys on how I could do that without looping? many thanks for any advice!

Viktor.w
  • 1,787
  • 2
  • 20
  • 46
  • Do all inner arrays have the same shape? – yatu May 28 '20 at 10:13
  • in the example yes, but in my source code no unfortunately... – Viktor.w May 28 '20 at 10:14
  • Here is the exact solution: https://stackoverflow.com/questions/43334040/python-append-two-matrix-side-by-side – Samip Poudel May 28 '20 at 10:15
  • It is not possible to do this **without** looping, but you can do it without **explicit** looping: `list(map(list, zip(a, b)))`, but it will not be buying you any significant speed-up. – norok2 May 28 '20 at 10:26

1 Answers1

1

You just need to use the zip built-in function:

[list(x) for x in zip(list1, list2)]

Similarly:

list(map(list, zip(list1, list2)))
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50