Python sets: difference() vs symmetric_difference()

Per https://www.programiz.com/python-programming/methods/set/symmetric_difference:

The symmetric difference of two sets A and B is the set of elements which are in either of the sets A or B but not in both.

However the difference of course, is self explanatory.


symmetric difference

If A and B are sets

A - B

is everything in A that's not in B.

>>> A = {1,2,3}
>>> B = {1,4,5}
>>> 
>>> A - B
{2, 3}
>>> B - A
{4, 5}

A.symmetric_difference(B) are all the elements that are in exactly one set, i.e. the union of A - B and B - A.

>>> A.symmetric_difference(B)
{2, 3, 4, 5}
>>> (A - B).union(B - A)
{2, 3, 4, 5}

The difference between two intersecting sets (or groups of things) is not exactly the same as the arithmetic difference.

intersection sets

Consider the two sets above (blues and greens) as being two sets, or circles, that intersect each other. The yellows being the intersection, what belongs to both sets.

Now consider what the set result from subtracting greens from blues should have. Should it have any greens? No, as they are subtracted. Should it have any yellows? No, because yellows are also greens.

And what about subtracting blues from the greens? The opposite is true, of course.

And so you can get items from one set or the other, but not from both. I want to introduce to you, my little friend, symmetric difference. This guy gives you the blues and greens, but not the yellows.

>>> a = {1,2,3}
>>> b = {1,4,5}
>>> a - b       ## asymmetric difference
{2, 3}
>>> b - a       ## asymmetric difference
{4, 5}
>>> a ^ b       ## symmetric difference
{2, 3, 4, 5}

The asymmetric difference depends on what you do with a and b, or how you look at them, in what order you compare them. Look at it one way you get one thing, look a different way you get a different thing. Where the symmetric difference, by definition, does not care which way you look at it.

Note. This is analogous behavior to that of a XOR. Hence the operator chosen in the python language. ^ is also used as a binary XOR if you give it numbers.