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]
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]
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]]
Numpy solution:
import numpy as np
a = [1, 2]
b = [3, 4]
joint_array = np.asarray((a, b)).T
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.
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']