Python sets: difference() vs symmetric_difference()

Question:

What is the difference between difference() and symmetric_difference() methods in python sets?

Asked By: Anya Samadi

||

Answers:

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}
Answered By: timgeb

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.

Answered By: RandomHash

The difference between two intersecting sets is not exactly the same as the arithmetic difference.

intersection sets

Consider the two circles above (blue and green) as two sets, or groups of things, that intersect each other (in yellow). Whatever is in yellow is merely so we can reference to them, in truthfulness they are both green and blue at the same time.

Now consider the following.

What should the set resulting from subtracting greens from blues have? Should it have any greens? No, as it’s the greens we want subtracted. Should it have any yellows? No, because yellows are greens.

And what about the opposite? Subtracting blues from greens. It should have no blues, and no yellows because yellows are blues.

So we can get things from one set or the other, but not those that differ. This is what symmetric difference is about.

Consider the example.

>>> a = {1,2,3}
>>> b = {1,4,5}
>>> a - b       ## asymmetric difference
{2, 3}                ## nothing from b here
>>> b - a       ## asymmetric difference
{4, 5}                ## nothing from a here
>>> a ^ b       ## symmetric difference
{2, 3, 4, 5}          ## from a and b but not from both

The asymmetric difference depends on what you do with a and b, or how you look at them, or in what order you compare them. Look at them 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.

Answered By: Pedro Rodrigues