views:

588

answers:

2

What's the pythonic way of achieving the following?

list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]

#Need to create a of tuples from list_a and list_b

list_c = [(1,5), (2,6), (3,7), (4,8)]

Each member of list_c is a tuple, whose first member is from list_a and the second is from list_b.

+7  A: 
>>> list_a = [1, 2, 3, 4]
>>> list_b = [5, 6, 7, 8]
>>> zip(list_a,list_b)
[(1, 5), (2, 6), (3, 7), (4, 8)]
S.Mark
you have to know that the zip function stops at the end of the shortest list, which may not be always what you want. the `itertools` module defines a `zip_longest()` method which stops at the end of the longest list, filling missing values with something you provide as a parameter.
Adrien Plisson
+2  A: 

Youre looking for the builtin function zip.

mizipzor