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 find complement in two lists

There are two phones, phoneA and phoneB, how to how to find complement in phoneB but not in phoneA, one ex;

phoneA =  ["long lasting battery”, ”clear display”, ”great camera”, ”storage space”], [“clear display”, ”long lasting battery”, ”great camera”, ”warp-speed word processing”]

phoneB =  ["long lasting battery”, ”clear display”, ”great camera”, ”storage space”], [“clear display”, ”long lasting battery”, ”great camera”, ”warp-speed word processing”, “great sound”]

I write code like this by python:

d = [x for x in phoneB if x not in phoneA]
print(d)

but the code is wrong, anyone has better idea?

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 :

You need to add the 2 lists together, then check them:

d = [x for x in phoneB[0]+phoneB[1] if x not in phoneA[0]+phoneA[1]]
>>> d
['great sound']

Or if you have multiple sublists, this will also work:

# This joins all the elements in sublists into one large list
d = [x for x in [j for i in phoneB for j in i]
     if x not in [j for i in phoneA for j in i]]
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