tags:

views:

55

answers:

1

How can I merge/combine two or three elements of a list. For instance, if there are two elements, the list 'l'

l = [(a,b,c,d,e),(1,2,3,4,5)]

is merged into

[(a,1),(b,2),(c,3),(d,4),(e,5)]

however if there are three elements

l = [(a,b,c,d,e),(1,2,3,4,5),(I,II,II,IV,V)] 

the list is converted into

[(a,1,I),(b,2,II),(c,3,III),(d,4,Iv),(e,5,V)]

Many thanks in advance.

+11  A: 

Use zip:

l = [('a', 'b', 'c', 'd', 'e'), (1, 2, 3, 4, 5)]
print zip(*l)

Result:

[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
Mark Byers
Thanks for your help. Btw, how do I remove inner parenthesis in: (a, (1,2)) so I get a list like: (a,1,2)? Thanks again.
DGT
@DGT You can use `itertools.chain`.
Beau Martínez