-1

I have two arrays:

a = [[11, 12], [21, 22]]
b = [[101, 102], [201, 202]]

my desire result after combining is:

[[[11,21], [12,22]],[[101, 201],[102,202]]]

is it a possible way to do the above in a simple way without using for or other type of looping? Thanks

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Are your _lists_ (not _arrays_) always 2x2? – DYZ Mar 15 '20 at 03:54
  • 4
    See https://stackoverflow.com/questions/6473679/transpose-list-of-lists for how to transpose the lists. Then return `[transpose(a), transpose(b)]` – Barmar Mar 15 '20 at 03:57
  • 1
    What have you tried? Folks are more likely to respond if you provide some information on what you've tried. Also, can you describe more generally what it is you are trying to do? Always two 2x2 arrays? pair first elements with first, 2nd elements with 2nd? – ViennaMike Mar 15 '20 at 03:58

2 Answers2

0

Try the following:

[list(map(list, zip(*a))), list(map(list, zip(*b)))]
Out: [[[11, 21], [12, 22]], [[101, 201], [102, 202]]]
William
  • 281
  • 1
  • 11
-1

If all you're doing simply combing the two arrays, you can combine them by creating a new array and set the elements of this new array as the elements of the past two arrays:

newArray = [a[0], a[1], b[0], b[1]]