-1

I have list1, list2 and list3 and I wnat to merge them to a single array without changing their key

I checked stackoverflow and python.org

list1 =[1,2,3]
list2=[4,5,6]
list3=[7,8,9]



final_expected_result =[[1,4,7],[4,5,8],[3,6,9]]

1 Answers1

1

I think that zip is what you're looking for. zip does return an iterable object, so you'll likely want to convert it to a list.

>>> list(zip(list1, list2, list3))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

If you're set on having a list of lists instead of a list of tuples integrate a map into the expression.

>>> list(map(list, zip(list1, list2, list3)))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Jacinator
  • 1,413
  • 8
  • 11
  • what's the point of zipping lists and converting back to `list`? That would just "cut" all lists to the length of the shortest list. `zip` could more likely be useful if you e.g. want to `for a,b,c in zip(list1, list2, list3):` – FObersteiner Sep 30 '19 at 18:54
  • @MrFuppes, I entirely agree. However, I've made a point of matching my output to the output specified by OP. Otherwise I wouldn't have bothered with the `map` either. – Jacinator Sep 30 '19 at 18:57
  • it's alright, I'm not even sure what he's asking for exactly. – FObersteiner Sep 30 '19 at 18:59