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

How to use python to merge two lists using common elements?

I have two lists in python, the first with each element composed by a string and an integer:

delta[0:10]
[('conhecimento', 17),
 ('ciência', 14),
 ('interdisciplinaridade', 13),
 ('saber', 10),
 ('objeto', 10),
 ('pode', 10),
 ('processo', 9),
 ('conceito', 9),
 ('assim', 8),
 ('mundo', 8)]

And a second list composed by a string and a tuple:

echo[0:10]
[('mundo', [2024]),
 ('assim', [2022]),
 ('conceito', [1599, 1602, 1862, 1865]),
 ('processo', [1949, 1963, 1972]),
 ('pode', [2018]),
 ('objeto', [1566, 1605]),
 ('saber', [2016]),
 ('interdisciplinaridade', [2014]),
 ('ciência', [2013,756]),
 ('conhecimento, [2011, 2223])]

Both lists have the same length, because they were made with the same dataset, so they all share the same string elements.

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

len(echo)
1398

len(delta)
1398

All string elements are present in both lists but in a different order. I need to build a third list where the first index is the common string present in both lists, it also has to be followed by the integer, as in the first list, and the respective tuple, associated with the string that is also present in the second list. In the end, I intend the final merged list to look like this:

 final[0:4]
 [('conhecimento', 17, [2011, 2223]),
  ('ciência', 14, [2013,756]),
  ('interdisciplinaridade', 13, [2014]),
  ('saber', 10, [2016])]

And also, if possible, I want a method to sort the elements of the final list considering the value of the second element and another method to sort these elements considering the highest value of the third element on the final list.

Thanks in advance!

>Solution :

You can do this way with iterating one list and make another list of tuple to dict where you can look up for values and finally append it like below-

delta = [
    ("conhecimento", 17),
    ("ciência", 14),
    ("interdisciplinaridade", 13),
    ("saber", 10),
    ("objeto", 10),
    ("pode", 10),
    ("processo", 9),
    ("conceito", 9),
    ("assim", 8),
    ("mundo", 8),
]

echo = [
    ("mundo", [2024]),
    ("assim", [2022]),
    ("conceito", [1599, 1602, 1862, 1865]),
    ("processo", [1949, 1963, 1972]),
    ("pode", [2018]),
    ("objeto", [1566, 1605]),
    ("saber", [2016]),
    ("interdisciplinaridade", [2014]),
    ("ciência", [2013, 756]),
    ("conhecimento", [2011, 2223]),
]


final = []
lookup = dict(echo)
for a, b in delta:
    final.append((a, b, lookup.get(a)))
print(final)

Output:

[
    ("conhecimento", 17, [2011, 2223]),
    ("ciência", 14, [2013, 756]),
    ("interdisciplinaridade", 13, [2014]),
    ("saber", 10, [2016]),
    ("objeto", 10, [1566, 1605]),
    ("pode", 10, [2018]),
    ("processo", 9, [1949, 1963, 1972]),
    ("conceito", 9, [1599, 1602, 1862, 1865]),
    ("assim", 8, [2022]),
    ("mundo", 8, [2024]),
]
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