Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine two list of tuples depending on name

I have two lists of tuples:

a=[(name_2,array_2),(name_7,array_7),...,(name_n,array_n)]
b=[(name_3,arr_3),(name_12,arr_12),...,(name_n,arr_n)]

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:

combined=[(name_1,array_1,arr_1),(name_2,array_2,arr_2),...,(name_n,array_n,arr_n)]

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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))]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading