-2

I have two one dimensional List say A and B. Each of the list has 2000 elements. I want to merge this two list in python to form tow dimensional array e.g.

A=[“Apple”,”Ant”,”Avocado”....]
B=[“Banana”,”Bear”,”Brinjal”,....]

Merge list should be 2000 rows in a way that each row will have two columns. As per above example first row will have “Apple” and “Banana”, second row will be “Ant” and “Bear”.

Tried X = A + B, however this does not work.

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40

1 Answers1

1

Following is one of the simplest solutions:

A = ["Apple", "Ant", "Avocado"]
B = ["Banana", "Bear", "Brinjal"]

result = [[A[i], B[i]] for i in range(len(A))]
print(result)

you can use this one liner too:

result = list(zip(A, B))

If you strictly want list of lists then try this:

result = [list(value) for value in zip(A,B)]
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39