What is the difference between difference() and symmetric_difference() methods in python sets?
-
4docs https://docs.python.org/2/library/sets.html#set-objects – Druta Ruslan Jun 15 '18 at 07:59
-
1The description in the docs is pretty clear. Try playing around with a few simple example sets. – PM 2Ring Jun 15 '18 at 07:59
-
Given sets `a` and `b`, `(a - b) | (b - a) == a ^ b`. Symmetry is achieved by including both differences. – Yann Vernier Jun 15 '18 at 08:04
3 Answers
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}

- 76,762
- 20
- 123
- 145
-
11I'll just add that operators `^`, `-`, `|`, and `&` provide symmetric difference, difference, union, and intersection operations respectively. Therefor, in the operations provided, the following is true: `a.symmetric_difference(b) == a ^ b` and `(a - b).union(b - a) == a - b | b - a`. – Pedro Rodrigues Jan 31 '20 at 17:45
The difference between two intersecting sets is not exactly the same as the arithmetic difference.
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.

- 2,520
- 2
- 27
- 26
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.

- 115,751
- 26
- 228
- 437

- 669
- 6
- 20
-
I hadn't pay attention to "either" key word in definition. thanks. – Anya Samadi Jun 15 '18 at 08:07
-
If you feel that ypur question has been answered by the community. Please select the correct answer. – RandomHash Jun 15 '18 at 08:11