29

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
  • 112,593
  • 23
  • 157
  • 280
Phillip
  • 3,094
  • 5
  • 24
  • 47

2 Answers2

54

I usually prefer a shortcut:

set(a) ^ set(b)
{2, 4, 6}
Gena Kukartsev
  • 1,195
  • 2
  • 15
  • 16
  • 1
    hi, out of interest what is the idea with the "^"? – Je Je Apr 20 '21 at 03:10
  • @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
15

Symmetric difference?

>>> set(a).symmetric_difference(b)
{2, 4, 6}
ubundows
  • 371
  • 5
  • 11
  • 4
    `symmetric_difference` takes in any iterable, so there's no need for the second call to `set`. – Alex Hall Oct 21 '16 at 20:59
  • ty! This is ANOTHER reason for preferring the method over the shortcut The other being: its much more explicit! btw: Looots of these take any iterable! – ewerybody Oct 01 '20 at 09:18
  • can this method be used for two pd df and extract non intersecting rows – Ayan Mitra Nov 05 '20 at 08:14