1
00:00:05,280 --> 00:00:09,040
Earlier in this section, when we looked 
at the discard method, we wanted to remove 

2
00:00:09,040 --> 00:00:13,280
restricted items from our packing list.
discard is useful when you want to remove 

3
00:00:13,280 --> 00:00:17,200
an item from a set, without worrying 
about whether it was present or not. 

4
00:00:17,200 --> 00:00:21,052
However, when you want to remove 
every item in one set, from another, 

5
00:00:21,052 --> 00:00:23,102
the set difference is more appropriate. 

6
00:00:23,440 --> 00:00:28,173
Open the packing_list.py file from that 
earlier video, and we'll make some changes. 

7
00:00:28,720 --> 00:00:32,512
We'd left this code using 
remove, which causes it to crash. 

8
00:00:37,120 --> 00:00:43,490
That's on line 53. I'll change that 
back to discard, so that the code works: 

9
00:00:50,800 --> 00:00:55,173
I'll run the program to make sure it works 
– and also remind ourselves what it's doing. 

10
00:00:57,600 --> 00:01:01,714
Enter 2, to travel by plane.
There's quite a lot of output, 

11
00:01:01,714 --> 00:01:03,736
so I'll move the Run pane to Right Top. 

12
00:01:06,640 --> 00:01:10,872
The restricted items have been removed from 
the packing list, which is what we wanted. 

13
00:01:11,120 --> 00:01:14,080
That's a good demonstration of the 
discard method, but it's not the 

14
00:01:14,080 --> 00:01:18,080
best way to perform this particular task.
When you want to remove everything that 

15
00:01:18,080 --> 00:01:21,675
exists in a set, you can avoid 
having to iterate over the set. 

16
00:01:21,760 --> 00:01:27,916
We do that by using the set difference operation.
I'll comment out that loop, and replace it with difference.

17
00:01:27,916 --> 00:01:32,238
 I'll use difference update, because 
we want to modify the original items set: 

18
00:01:38,000 --> 00:01:42,370
I've used the operator first, this 
time, just to mix things up a bit. 

19
00:01:42,480 --> 00:01:47,940
We'll do the same thing with the method, 
next. Before we do that, run the program: 

20
00:01:50,400 --> 00:01:57,084
Enter option 2, and once again, all the restricted 
items have been removed from our items set. 

21
00:01:57,360 --> 00:02:01,857
That's a lot more efficient. We've 
replaced a loop with a single line of code. 

22
00:02:02,000 --> 00:02:05,600
Using the method is very similar. 
I'll comment out that line, 

23
00:02:05,600 --> 00:02:08,606
and call the difference_update method instead:

24
00:02:18,084 --> 00:02:19,730
Run the program again,

25
00:02:21,760 --> 00:02:23,167
and enter 2 again. 

26
00:02:24,240 --> 00:02:28,537
We get the same result – all restricted 
items are removed from the items set. 

27
00:02:28,537 --> 00:02:33,360
If you find yourself looping over the items in a 
set, and performing some operation on another set, 

28
00:02:33,360 --> 00:02:37,280
check out the set operations.
They're very efficient, and can save 

29
00:02:37,280 --> 00:02:42,961
you writing loops. Writing less code can mean 
introducing fewer bugs, which is a good thing. 

30
00:02:43,040 --> 00:02:46,548
Also remember that you can pass any 
iterable to the operation methods. 

31
00:02:46,960 --> 00:02:52,810
In this case, the code would still work fine, 
if restricted_items is a list rather than a set. 

32
00:02:53,120 --> 00:02:58,080
The last set operation to look at is symmetric 
difference. See you in the next video.

