I want to intersect two lists (with NOT), and return the elements of list A that are not present in list B.
example:
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) ????? set(b))
[2, 4]
I want to intersect two lists (with NOT), and return the elements of list A that are not present in list B.
example:
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) ????? set(b))
[2, 4]
You are looking for the set difference; the -
operator will do that for you:
list(set(a) - set(b))
If you use the set.difference()
method the second operand does not need to be a set, it can be any iterable:
list(set(a).difference(b))
Demo:
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a).difference(b))
[2, 4]