Pergunta

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]
Foi útil?

Solução

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]

Outras dicas

Something like this?

>>> list(set(a) - set(b))
[2, 4]
a = [1,2,3,4,5]
b = [1,3,5,6]
print list(set(a) - set(b))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top