Sun Bear
Sun Bear

Reputation: 8297

Combine 2 lists into 1 list with tuple elements

May I know the python built-in function to do the following? That is, combine 2 lists into 1 list such that the elements of each list is used to form a tuple element in the new list. Thank you.

>>> a = ['1','2','3']
>>> b = ['x','y','z']
>>> c = []
>>> for i, val in enumerate(a):
    c.append( (i, b[i]) )
>>> c
[(0, 'x'), (1, 'y'), (2, 'z')]
>>> 

Upvotes: 2

Views: 157

Answers (1)

U13-Forward
U13-Forward

Reputation: 71610

It is zip:

>>> list(zip(a,b))
[('1', 'x'), ('2', 'y'), ('3', 'z')]

Upvotes: 5

Related Questions