Pratheek Hb
Pratheek Hb

Reputation: 13

Array concatenation in Python

I am new to Python and I would like to combine two arrays. I have two arrays: A and B and would like to get array C as follows:

A = [1, 2, 3, 4, 5] 
B = [6, 7, 8, 9, 10]

Result array:

C = [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

Upvotes: 0

Views: 570

Answers (2)

Blusky
Blusky

Reputation: 3784

Without any dependency:

>>> A = [1, 2, 3, 4, 5]
>>> B = [6, 7, 8, 9, 10]
>>> C = [(A[i], B[i]) for i in range(len(A))]
>>> C
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

Upvotes: 0

user2390182
user2390182

Reputation: 73450

Use zip, the built-in for pairwise iteration:

C = list(zip(A, B))

Note that you are not concatenating the two lists.

Upvotes: 2

Related Questions