39

So I know how to find the intersection of two lists by doing:

>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]

But what is the best way to find all the elements that are not included in the intersection. My initial idea is to create a union of the two lists and then remove all the elements from the intersection from the union, as such:

>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> intersection = list(set(a) & set(b))
>>> union = list(set(a) | set(b))
>>> non_intersection = intersection - union
[2, 4, 6]

Is this the best way to do this or is there another way?

martineau
  • 119,623
  • 25
  • 170
  • 301
Phil
  • 3,342
  • 5
  • 28
  • 50

2 Answers2

66

I usually prefer a shortcut:

set(a) ^ set(b)
{2, 4, 6}
Gena Kukartsev
  • 1,515
  • 2
  • 17
  • 19
  • 1
    hi, out of interest what is the idea with the "^"? – Je Je Apr 20 '21 at 03:10
  • 1
    @JeJe The `^` operator in this case is similar to the "XOR" operator for bitwise functions, but for set mathematics. Here it is finding the symmetric difference between two sets, returning a new set composed of only the items that are included in set A or set B, but not both. – h0r53 Sep 08 '21 at 17:12
19

Symmetric difference?

>>> set(a).symmetric_difference(b)
{2, 4, 6}
ubundows
  • 421
  • 6
  • 11