1

Possible Duplicate:
Python - merge items of two lists into a list of tuples

There are two lists

x = [1,2,3,4,5]
y = ['a','b','c','d','e']

How can I get the list

z = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
Community
  • 1
  • 1
Bobby
  • 11
  • 1
  • This is a duplicate of http://stackoverflow.com/questions/2407398/python-merge-items-of-two-lists-into-a-list-of-tuples – Mark Longair Mar 06 '11 at 08:28

3 Answers3

3
z = zip(x,y)

will do your job.

>>> x = [1,2,3,4,5]
>>> y = ['a','b','c','d','e']
>>> z = zip(x,y)
>>> z
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
2

Given your input, the builtin function zip(x, y) will provide the output, that you want.

see: http://docs.python.org/library/functions.html#zip

cypheon
  • 1,023
  • 1
  • 13
  • 22
1
x = [1,2,3,4,5]
y = ['a','b','c','d','e']
print zip(x,y)
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93