1

Let's say, I have two lists:

[a, b] and [c,d]

How can I get the following array as a result in Python?

[a, c]
[b, d]

4 Answers4

7

Zip those lists.

list(map(list, zip(list1,list2)))

IDLE Output:

>>> list1 = [1,2]
>>> list2 = [10,11]
>>> list(map(list, zip(list1,list2)))
[[1, 10], [2, 11]]
gsb22
  • 2,112
  • 2
  • 10
  • 25
6

Numpy solution:

import numpy as np

a = [1, 2]
b = [3, 4]
joint_array = np.asarray((a, b)).T
user2653663
  • 2,818
  • 1
  • 18
  • 22
3

Since you specifically mentioned in the comments that you don't want a list of lists but rather a concatenation of the two lists:

[a,b]+[c,d]

the result will be [a,b,c,d]

otherwise if this is not what you wanted Gauri's or user2653663's answer might be what you want.

kinshukdua
  • 1,944
  • 1
  • 5
  • 15
0

If you mean to create two new list variables with the columnwise concatenations you can do this:

list1 = ["a", "b"]
list2 = ["c", "d"]

list3,list4 = [list(z) for z in zip(list1,list2)]
print(list3) 
print(list4) 

# ['a', 'c']
# ['b', 'd']
Alain T.
  • 40,517
  • 4
  • 31
  • 51