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

Python compare words and remove duplicates in two list of lists

I have the following lists:

list_1_test = [['hi','there','how'],['we','are','one']]
list_2_test = [['hi','you','how'],['we','not','one']]

I wish to compare the words in the list and get the following output:

list_3_test = [['there','you'],['are','not']]

I know how to do this in a simple list, for example:

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

list_1_test = ['hi','there','how']
list_2_test = ['hi','you','how']

list_3_test=[]
for i in list_1_test:
    if i not in list_2_test:
        list_3_test.append(i)

for i in list_2_test:
    if i not in list_1_test:
        list_3_test.append(i)   

and the result is

['there', 'you']

But when it comes to list of lists, my brain is fried. The order matters. Any help is much appreciated.

>Solution :

If order doesn’t matter, you can use set operations + list comprehension:

out = [list(set(l1).union(l2) - set(l1).intersection(l2)) for l1, l2 in zip(list_1_test, list_2_test)]

Output:

[['you', 'there'], ['are', 'not']]

If order matters, you can use dict.fromkeys:

out = []
for l1, l2 in zip(list_1_test, list_2_test):
    one = dict.fromkeys(l1).keys()
    two = dict.fromkeys(l2).keys()
    out.append(list(one - two) + list(two - one))

Output:

[['there', 'you'], ['are', 'not']]
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