1
00:00:05,280 --> 00:00:10,640
In this video, we'll have a look at the last of 
the four set operations, symmetric difference. 

2
00:00:10,640 --> 00:00:15,600
The example is going to be a bit contrived, 
because I'm trying to make it non-maths related. 

3
00:00:15,600 --> 00:00:20,720
Outside of mathematics, you may not need to find 
the symmetric difference of sets very often. 

4
00:00:20,720 --> 00:00:27,455
Python supports the operation, so we'll have a
look at what it can do, and it's there if you need it.

5
00:00:27,455 --> 00:00:30,321
For this example, we've got two sets.

6
00:00:30,321 --> 00:00:33,680
They represent programming 
courses at a training establishment. 

7
00:00:33,680 --> 00:00:38,280
We'd like to learn more than one language, 
and we're determined not to miss any lessons. 

8
00:00:39,440 --> 00:00:44,000
That means we'll have to take one course in 
the mornings, and another in the afternoons. 

9
00:00:44,000 --> 00:00:48,707
Which means we can't take any course that has 
lessons in both the morning and the afternoon. 

10
00:00:51,040 --> 00:00:54,880
We've seen the set intersection before.
We can use that to see which courses 

11
00:00:54,880 --> 00:00:59,120
have lessons in the morning and afternoon.
The green area represents the intersection 

12
00:00:59,120 --> 00:01:02,934
of the two sets, and shows us 
which courses we can't take. 

13
00:01:04,319 --> 00:01:09,360
There are only 6 courses in this example, so 
it's not hard to see which courses we can take. 

14
00:01:09,440 --> 00:01:15,280
Bear with me on this, and imagine sets that 
could contain hundreds (or thousands) of items. 

15
00:01:15,280 --> 00:01:19,520
To get the set of courses we can take, 
we want the opposite of the intersection. 

16
00:01:22,240 --> 00:01:25,440
The symmetric difference is 
the opposite of intersection. 

17
00:01:25,440 --> 00:01:30,400
It produces the set of items that are in 
one set, or the other, but not in both. 

18
00:01:30,400 --> 00:01:36,800
Java, C, C#, and Ruby are in both sets, so 
they're excluded from the symmetric difference. 

19
00:01:36,800 --> 00:01:40,480
That leaves Python and Lisp, 
which is exactly what we wanted. 

20
00:01:40,480 --> 00:01:44,201
So lets see how we produce the 
symmetric difference in code. 

21
00:01:45,040 --> 00:01:49,680
I've created a new Python file 
called set_sd.py for this example. 

22
00:01:49,680 --> 00:01:52,867
The name set_symmetric_difference is a bit long, 

23
00:01:52,867 --> 00:01:58,561
so I've abbreviated it. Pause the video, 
while you create your Python file. 

24
00:01:59,120 --> 00:02:02,094
We start with the two sets 
that contain the courses: 

25
00:02:39,040 --> 00:02:43,880
The symmetric difference operator is the ^ 
symbol, and I'll start with the operator: 

26
00:02:50,640 --> 00:02:54,080
We could also use the method, 
and we'll do that next. 

27
00:02:54,080 --> 00:02:57,254
First, I'll print the possible 
courses that we can take: 

28
00:03:03,897 --> 00:03:05,454
and run the program. 

29
00:03:08,480 --> 00:03:12,560
There shouldn't be any surprises with 
the output, we get Lisp and Python. 

30
00:03:12,560 --> 00:03:17,600
Trying to do that, by iterating over the 
sets, would be quite fiddly. Have a go, 

31
00:03:17,600 --> 00:03:22,480
if you want to check how messy the code would be.
I won't type the code to do that, on video, 

32
00:03:22,480 --> 00:03:28,000
because you just wouldn't do it that way. At 
least, I don't recommend that you do it that way. 

33
00:03:28,000 --> 00:03:31,360
Even if these courses were stored 
in lists, rather than sets, 

34
00:03:31,360 --> 00:03:36,400
you can still produce the symmetric difference. 
You'd convert one list to a set, and pass the 

35
00:03:36,400 --> 00:03:41,894
other list to the symmetric_difference method.
That's worth seeing, so I will do that. 

36
00:03:42,640 --> 00:03:47,574
First, we change lines 1 and 2 so that 
we've got lists, rather than sets. 

37
00:03:56,000 --> 00:04:01,187
We've now got a warning from IntelliJ, because 
the ^ operator isn't defined for lists. 

38
00:04:01,187 --> 00:04:04,142
We have to use the method instead:

39
00:04:10,265 --> 00:04:15,401
That's removed the warning. The symmetric difference operator is defined for the set.

40
00:04:15,401 --> 00:04:17,440
But we've got another warning now.

41
00:04:17,440 --> 00:04:22,960
We can't use the operator with a set and a list.
We could convert afternoon to a set as well. 

42
00:04:22,960 --> 00:04:27,840
Or we could use the method instead. 
I'll change the code to use the method: 

43
00:04:39,520 --> 00:04:44,720
As we've seen, an advantage of the methods is that 
we can pass any iterable to them. That avoids the 

44
00:04:44,720 --> 00:04:50,054
need to explicitly convert afternoon to a set.
Ok, run the program: 

45
00:04:51,920 --> 00:04:55,920
and we get the same result. Remember 
that the order might be different, 

46
00:04:55,920 --> 00:05:00,000
because sets have no ordering.
Unlike difference, symmetric 

47
00:05:00,000 --> 00:05:04,080
difference is commutative – you can 
perform the operation either way round. 

48
00:05:04,080 --> 00:05:06,790
So we could also write that as:

49
00:05:27,481 --> 00:05:30,201
Run the program again

50
00:05:30,800 --> 00:05:35,440
and once again, we get the same result.
As with the other set operations, 

51
00:05:35,440 --> 00:05:41,840
there's also a symmetric_difference_update 
operation. The operator is ^=, if you prefer to 

52
00:05:41,840 --> 00:05:46,640
use the operators rather than call the methods.
And that's symmetric difference. 

53
00:05:46,640 --> 00:05:49,053
If you find that you need 
to do something like this, 

54
00:05:49,053 --> 00:05:54,000
the symmetric difference operation will save a 
lot of code – and reduce the risk of adding a bug. 

55
00:05:54,080 --> 00:05:58,560
It's one more tool in your toolkit. And if 
you do find a non-mathematical use for it, 

56
00:05:58,560 --> 00:06:02,360
please let us know in the Q&A.
In the next few videos, 

57
00:06:02,360 --> 00:06:06,640
we'll look at super-sets and 
sub-sets. See you in the next video.

