1
00:00:05,520 --> 00:00:08,400
Okay, let's have a look at 
some code that we might use, 

2
00:00:08,400 --> 00:00:11,520
to delete restricted items from a packing list. 

3
00:00:11,520 --> 00:00:15,520
I've created a new Python 
file called packing_list.py. 

4
00:00:15,520 --> 00:00:21,141
There's a lot of data to type, and typing lists 
of items is boring, so download packing_list.txt 

5
00:00:21,141 --> 00:00:26,160
from the resources for this video, and copy 
and paste the code into your Python file. 

6
00:00:26,160 --> 00:00:30,080
Pause the video while you do that.
We'll let the user decide if they're going 

7
00:00:30,080 --> 00:00:36,211
to travel by car, or by plane. I've put those 
options into a dictionary, on line 1 of the code. 

8
00:00:36,560 --> 00:00:40,400
Next, we've got a set of items 
that we'll need, while camping. 

9
00:00:40,400 --> 00:00:44,720
We're not wild camping – we'll be eating in the 
restaurant on the campsite – so please don't ask 

10
00:00:44,720 --> 00:00:49,680
where the pans, plates and cutlery are .
Below that, and I'll scroll line 29 to 

11
00:00:49,680 --> 00:00:51,976
the top of the screen, 

12
00:00:52,800 --> 00:00:56,000
we've got some of the items 
that you can't take on a plane. 

13
00:00:56,000 --> 00:01:01,256
Not in your hand luggage, anyway. You can take 
some of these items in your checked baggage. 

14
00:01:01,360 --> 00:01:03,120
But we're travelling light. 

15
00:01:03,120 --> 00:01:08,936
That's the data – the code starts on line 
39 – and I'll scroll up so we can see it. 

16
00:01:09,680 --> 00:01:14,611
Lines 39 to 43 print the menu, 
using our travel_mode dictionary. 

17
00:01:14,800 --> 00:01:19,120
That's code you've seen a few times 
now, and doesn't need any explanation. 

18
00:01:19,120 --> 00:01:24,110
I've included the alternative line, for 
students using Python 3.5 or earlier. 

19
00:01:24,240 --> 00:01:28,972
f-strings aren't available in those versions, 
and you'll have to use .format instead. 

20
00:01:29,040 --> 00:01:34,560
But you'll be used to that by now.
Lines 45 to 47 get the user's choice. 

21
00:01:34,560 --> 00:01:37,353
That's all simple stuff, so far. 

22
00:01:37,680 --> 00:01:43,440
At the end of the code, that's on lines 55 
to 58, we print out the set of items to pack. 

23
00:01:43,440 --> 00:01:47,840
I've sorted the set, so technically we're 
printing a list there. There's no real need 

24
00:01:47,840 --> 00:01:52,160
to sort the set, but it'll make it easier 
to check that items have been removed. 

25
00:01:52,160 --> 00:01:57,360
Okay, what we're really interested in, is what 
happens when the user chooses to go by plane. 

26
00:01:57,360 --> 00:02:01,440
In that case, we need to remove 
any restricted items from the set. 

27
00:02:01,440 --> 00:02:05,104
We'll add the code to do 
that, shortly. At the moment, 

28
00:02:05,104 --> 00:02:07,680
we've got a dummy pass inside the if block. 

29
00:02:07,680 --> 00:02:13,289
Before we look at the code to remove items, 
let's run the program, to see what it does. 

30
00:02:14,240 --> 00:02:18,000
We get the options printed, and 
the program's waiting for input. 

31
00:02:18,000 --> 00:02:21,840
When I enter 1, the program 
prints the items we should pack. 

32
00:02:24,240 --> 00:02:26,597
Okay, run it again: 

33
00:02:27,760 --> 00:02:32,640
and choose option 2.
At the moment, we get the same set of items. 

34
00:02:32,640 --> 00:02:38,320
My stove uses liquid fuel — a bit like petrol — 
and that's going to cause problems at the airport. 

35
00:02:38,320 --> 00:02:42,880
The knife will also raise a few eyebrows, 
as will the razor blades and scissors. 

36
00:02:42,880 --> 00:02:46,560
We need to remove any restricted 
items when travelling by plane. 

37
00:02:46,560 --> 00:02:51,056
Close the Run pane, and we'll 
see the code to do that. 

38
00:02:51,443 --> 00:02:55,540
I'll replace pass with the code to 
iterate over the restricted items: 

39
00:03:00,000 --> 00:03:04,654
Next, we'll use discard to remove 
the item from our packing list: 

40
00:03:11,360 --> 00:03:15,680
Alright, this is an example of when you might 
want to use discard. We don't care whether 

41
00:03:15,680 --> 00:03:20,323
restricted items are in our packing 
list or not. We just want them gone. 

42
00:03:20,323 --> 00:03:25,360
Line 52 will remove restricted items if 
they're in the set, and do nothing otherwise. 

43
00:03:25,360 --> 00:03:31,840
So does it work ? Run the 
program, and choose option 2. 

44
00:03:33,680 --> 00:03:38,560
We sorted the set before printing the items it 
contains, so it's easy to see that the fuel, 

45
00:03:38,560 --> 00:03:43,440
the knife, and the other restricted 
items, are no longer in our packing list. 

46
00:03:43,440 --> 00:03:47,040
Alright, that's how you'd use discard.
Note that this won't work 

47
00:03:47,040 --> 00:03:51,600
if we use remove instead.
Let's see the problem. I'll change discard 

48
00:03:51,600 --> 00:03:56,370
to remove, on line 52. I'll add a comment as well:

49
00:04:05,788 --> 00:04:10,664
Run the program, and choose option 2 again.

50
00:04:10,995 --> 00:04:15,692
We get an error, when we try to remove the 
first restricted item that we haven't packed. 

51
00:04:15,760 --> 00:04:19,878
That happened when the code tried to use 
remove to remove something that didn't exist. 

52
00:04:20,000 --> 00:04:24,281
Because sets aren't ordered, you may well 
get a different item causing the crash. 

53
00:04:24,281 --> 00:04:28,480
When we iterate over a set, we don't know 
the order that the items will appear. 

54
00:04:28,480 --> 00:04:32,370
Sets have no ordering, remember!
So our code crashed, 

55
00:04:32,370 --> 00:04:37,520
because it tried to do something that didn't need 
to be done. In this case, it tried to remove a 

56
00:04:37,520 --> 00:04:42,080
restricted item that we hadn't packed.
That's not good, and demonstrates why 

57
00:04:42,080 --> 00:04:48,320
discard is more appropriate than remove, here.
Ok, I'll finish this video with an observation. 

58
00:04:48,320 --> 00:04:51,600
We've used this example to 
demonstrate using discard. 

59
00:04:51,600 --> 00:04:55,680
In this case, where we're discarding all items 
from another set, there's another approach 

60
00:04:55,680 --> 00:04:59,280
that you should seriously consider.
So this is an example about how to 

61
00:04:59,280 --> 00:05:04,080
use discard. It demonstrates deleting 
items from a set, in a situation where 

62
00:05:04,080 --> 00:05:09,200
you don't care if the item is present or not.
We could also have used the set difference. 

63
00:05:09,200 --> 00:05:14,014
We'll come back to this example, when we 
look at set difference, in a later video. 

64
00:05:14,014 --> 00:05:18,560
In the next video, we'll see a situation where 
remove might be more appropriate than discard, 

65
00:05:18,560 --> 00:05:23,840
for what we need to do.
See you in the next video.

