how to zip two lists of tuples in python

list, python, tuples

Solution

def merge(a,b):
    for ax, (first, bx) in zip(a,b):
        if ax[0] != first:
            raise ValueError("Items don't match")
        yield ax + (bx,)

print list(merge(a,b))
print list(merge(merge(a,b),c))

Problem

I have two lists of tuples, for example: ``` a = [(1,2,3),(4,5,6),(7,8,9)] b = [(1,'a'),(4,'b'),(7,'c')] ``` The first element of each tuple in a and b are matched, I want to get a list like this: ``` merged = [(1,2,3,'a'),(4,5,6,'b'),(7,8,9,'c')] ``` Perhaps I will have another list like: ``` c = [(1,'xx'),(4,'yy'),(7,'zz')] ``` and merge it to "merged" list later, I tried "zip" and "map" which are not right for this case.

Original source