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

Difference between two lists of lists in Python

I have two lists of lists:

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[1,2,3],[9,9,9]]

I would like to get a set difference between them – expected outcome:

c = a - b = [[4,5,6],[7,8,9]].

I tried set() and set.difference() but it seems not to be able to compare lists.

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 :

Just use list comprehensions like so:

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[1,2,3],[9,9,9]]
c = [d for d in a if d not in b]
print(c)

Output:

[[4, 5, 6], [7, 8, 9]]
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