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

Taking the union of 2 lists of lists in Python

I have 2 list of lists.

One is say list1, which is :

[['1', '2', '*', '2', '1', '0.8'],
 ['1', '2', '3', '1', '1', '0.7'],
 ['*', '*', '3', '4', '1', '0.5'],
 ['1', '2', '*', '1', '1', '0.3'],
 ['2', '2', '*', '2', '1', '0.1']]

And list2 is :

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

[['3', '*', '1', '4', '1', '0.9'],
 ['1', '2', '2', '2', '1', '0.4'],
 ['2', '2', '*', '2', '1', '0.1']]

Now I want to get the union of these 2 list of lists and create a 3rd list of list and as ['2', '2', '*', '2', '1', '0.1'] this list is there in both of them so I want this to be in the final list of list for only 1 time.

I am doing:

final_list=list1 + list2

Final list is producing :

[['3', '*', '1', '4', '1', '0.9'],
 ['1', '2', '*', '2', '1', '0.8'],
 ['1', '2', '3', '1', '1', '0.7'],
 ['*', '*', '3', '4', '1', '0.5'],
 ['1', '2', '2', '2', '1', '0.4'],
 ['1', '2', '*', '1', '1', '0.3'],
 ['2', '2', '*', '2', '1', '0.1'],
 ['2', '2', '*', '2', '1', '0.1']]

My desired outcome is :

[['3', '*', '1', '4', '1', '0.9'],
 ['1', '2', '*', '2', '1', '0.8'],
 ['1', '2', '3', '1', '1', '0.7'],
 ['*', '*', '3', '4', '1', '0.5'],
 ['1', '2', '2', '2', '1', '0.4'],
 ['1', '2', '*', '1', '1', '0.3'],
 ['2', '2', '*', '2', '1', '0.1']]

>Solution :

You should be able to create a set, add each element of each list to that set, then create a list from that set, something like:

new_set = set()
for item in list1:
    set.add(item)
for item in list2:
    set.add(item)
final_list = list(set)

It won’t be guaranteed to be in order but it will have only unique elements. Of course, if you don’t care about order, you may as well be using sets exclusively so you don’t have to concern yourself with duplicates.


If you want to maintain order, you just maintain both a final list and the set, and only add to the final list if it’s not already in the set:

dupe_set = set()
final_list = []
for item in list1:
    if item not in dupe_set:
        set.add(item)
        final_list.append(item)
for item in list1:
    if item not in dupe_set:
        set.add(item)
        final_list.append(item)
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