Combine two list of tuples depending on name

I have two lists of tuples:


I want to combine them depending on their name which is the first value of each tuple in each set. The lists are not sorted by name. The result shall look like:


Is there any more effective solution than iterating with two pointers?

>Solution :

sorted_a = sorted(a, key=lambda x: x[0])
sorted_b = sorted(b, key=lambda x: x[0])

combined = [(sorted_a[idx][0], sorted_a[idx][1], sorted_b[idx][1]) for idx in range(len(a))]

Leave a Reply