Python sets: difference() vs symmetric_difference()

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

3

3 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} 
1

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

intersection sets

Consider the two sets above (blue and green) as being two sets, or circles, that intersect each other. The yellow part being the intersection, what belongs to both sets.

Now consider what the set resulting from subtracting the greens from the blues should have. Should it have any greens? No. It will have blues that are not greens (or are not yellows, in the same logic). This is also true the other way around.

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. The gives you 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 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 asymmetric 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.

Per :

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like