1
00:00:05,280 --> 00:00:10,080
In this video, we'll look at a couple of ways to
remove items from a dictionary, and discuss when

2
00:00:10,080 --> 00:00:15,440
you might use each one. That'll give us the basic
operations we can perform with the dictionary, and

3
00:00:15,440 --> 00:00:20,240
we can move on to using them for more interesting
things. We've seen how to delete an item from a

4
00:00:20,240 --> 00:00:26,800
list. using the del keyword. We first used that in
the lecture Replacing a slice, back in the lists

5
00:00:26,800 --> 00:00:31,280
and tuples section. So review those lectures
if you've forgotten how to delete from a list.

6
00:00:32,400 --> 00:00:38,053
Deleting from a dictionary is very similar, but
we use the key rather than an index position.

7
00:00:38,053 --> 00:00:40,720
So to see that in operation, what
I'm going to do is get rid

8
00:00:40,720 --> 00:00:46,756
of my starfighter. Who needs two jets after
all? So on line 19, I'm going to type del

9
00:00:46,756 --> 00:00:55,772
vehicles square brackets double quotes
and starfighter, and if I run this now,

10
00:00:56,160 --> 00:01:00,000
you can see that the starfighter has
now been removed from the dictionary.

11
00:01:00,880 --> 00:01:07,105
So that's easy and obvious, so why does Python
provide another way? Well if you've read 

12
00:01:07,105 --> 00:01:13,595
The Zen of Python, you know that there should be one -
and preferably only one - obvious way to do it. 

13
00:01:13,595 --> 00:01:19,760
If you haven't read The Zen of Python, it's a tongue-in-cheek
summary of Python's guiding principles.

14
00:01:19,760 --> 00:01:27,499
You could google for it, or create a new
Python file, so I'm going to call mine zen.py.

15
00:01:31,840 --> 00:01:34,720
You only need one line in
the file: import this.

16
00:01:37,680 --> 00:01:39,840
If we run it,

17
00:01:43,760 --> 00:01:50,080
and there it is - that's The Zen of Python printed
out. Now as I said, it's slightly humorous, 

18
00:01:50,080 --> 00:01:53,920
but there are some important principles in
there. You'll hear Python programmers

19
00:01:53,920 --> 00:01:58,947
referring to it, so this is a good time to check
it out. Pause the video now while you read it,

20
00:01:58,947 --> 00:02:03,564
then come back to dict_intro.py
when you're ready to continue.

21
00:02:04,960 --> 00:02:12,640
Alright. So let's see why we need another way
to remove items from the dictionary, or one

22
00:02:12,640 --> 00:02:19,120
reason anyway. What if we try to remove something
that doesn't exist? So I'll start out on line 20

23
00:02:19,120 --> 00:02:25,545
by removing my formula one racing
car, which I think has the key f1.

24
00:02:25,545 --> 00:02:30,199
Same deal there for double quotes and
the square brackets. If I run this now,

25
00:02:31,520 --> 00:02:36,800
we can see that the program crashes. Accessing
a dictionary with a key that doesn't exist

26
00:02:36,800 --> 00:02:42,937
will crash your program. So that's
one reason why we've got a get method,

27
00:02:44,800 --> 00:02:50,000
and that's to retrieve values that might not exist.
Now we could wrap all our deletions in a test,

28
00:02:50,000 --> 00:02:54,320
to make sure that the key exists, but
Python provides a more efficient way.

29
00:02:54,320 --> 00:02:59,879
Lists and dictionaries have a pop method. So
what I'm going to do is comment out line 20,

30
00:02:59,879 --> 00:03:07,525
and instead, on the next line type vehicles
dot pop parentheses double quotes f1.

31
00:03:08,596 --> 00:03:14,480
Okay, we'll run the program, and you can see
that it's crashed in exactly the same way.

32
00:03:14,480 --> 00:03:19,280
That's deliberate - it means you can use pop in
cases where you've already checked that 

33
00:03:19,280 --> 00:03:24,625
the key exists. You can also use it if you want to
get an error. Deleting that Justin Bieber song 

34
00:03:24,625 --> 00:03:29,920
that somehow found its way onto your playlist,
is a very important operation. Now if you spelled

35
00:03:29,920 --> 00:03:35,120
Bieber incorrectly, it's better that you get
an error so that you can correct the mistake.

36
00:03:35,120 --> 00:03:40,480
If your code fails silently, your playlist
might remain embarrassing. If you can't be

37
00:03:40,480 --> 00:03:45,200
sure that the key exists, and don't want to get
an error, you can pass the default value that

38
00:03:45,200 --> 00:03:55,200
pop will return, instead of crashing. So we can
come up here on line 21 and put a comma None.

39
00:03:55,200 --> 00:04:00,381
Run the program again, and this time it won't crash.
So we can see that this time it doesn't crash -

40
00:04:00,381 --> 00:04:05,200
we've just got our regular output. So the pop
method removes an item from the dictionary,

41
00:04:05,200 --> 00:04:12,000
and returns the value. If the key doesn't exist,
it returns whatever you pass for the default instead.

42
00:04:12,000 --> 00:04:17,600
Now we haven't used the return value but we could.
So let's change that. What I'll do instead,

43
00:04:17,600 --> 00:04:25,566
is online 21, result is equal to vehicles.pop, so
the rest of that line. Then on the next line, print,

44
00:04:26,400 --> 00:04:33,459
result in parentheses, and we'll just do another
print there to make some space. Run the program now,

45
00:04:34,880 --> 00:04:38,480
and we can see at the top there, we've got None
printed out as the first line of the output.

46
00:04:39,360 --> 00:04:43,760
Now often you'll pass None as the default, as
we've done here, but you can pass something

47
00:04:43,760 --> 00:04:49,908
more appropriate to your application.
As an example, I'll provide a string instead.

48
00:04:59,154 --> 00:05:02,345
Okay, now if we run this,

49
00:05:04,320 --> 00:05:08,160
this time you can see we get that
message printed out instead of None.

50
00:05:08,160 --> 00:05:13,920
So let's see what happens when we use pop with
keys that do exist. I really want a racing car,

51
00:05:13,920 --> 00:05:18,480
so I'm going to get rid of our Learjet. So
what I'm going to do now is, after line 22,

52
00:05:19,520 --> 00:05:27,769
I'm going to type plane is equal to vehicles
dot pop parentheses learjet in lowercase,

53
00:05:28,720 --> 00:05:35,600
print on the next line, plane, parentheses
plane. Then on line 26, bike is equal to vehicles

54
00:05:35,600 --> 00:05:43,680
dot pop parentheses double quotes tenere
comma double quotes not present.

55
00:05:45,440 --> 00:05:48,000
Let's also print out bike.

56
00:05:49,840 --> 00:05:54,081
Run the program and we'll check the first
three lines of output, at the top there.

57
00:05:54,081 --> 00:05:58,320
So the code on line 21 attempts to pop a
key that doesn't exist. We get the default

58
00:05:58,320 --> 00:06:03,120
value printed out - the string that we
passed to the pop method. On line 23,

59
00:06:03,920 --> 00:06:08,800
we're popping the learjet from the dictionary, so pop
returns the value and we can see that printed 

60
00:06:08,800 --> 00:06:16,720
in the second line of the output. I included line 26
to emphasize how the default value could be used.

61
00:06:16,720 --> 00:06:21,520
We passed a default value of not present, but
because the tenere is present, we get the value

62
00:06:21,520 --> 00:06:26,640
for the tenere printed out - that's the third line
of the output. The default value is only returned

63
00:06:26,640 --> 00:06:32,480
when the key doesn't exist in the dictionary. Okay,
so that's two different ways to delete items from

64
00:06:32,480 --> 00:06:38,000
a dictionary. del performs slightly better than
pop, but if you want to use one or other form of pop

65
00:06:38,000 --> 00:06:42,560
and ignore del, that's not a bad strategy. You'd
need a very large dictionary, and be performing

66
00:06:42,560 --> 00:06:48,480
lots of deletions, for the performance differences
to be noticeable. I'll focus on using pop in the

67
00:06:48,480 --> 00:06:53,200
examples, partly because del describes what it
does so well. del is short for delete, which is

68
00:06:53,200 --> 00:06:58,514
easy to remember. You're unlikely to have problems
working it out when you come across it. So at this point,

69
00:06:58,514 --> 00:07:02,960
we've now covered the basic operations
you can perform with a dictionary. In the next

70
00:07:02,960 --> 00:07:08,560
few videos, we'll see some more realistic examples
of using dictionaries. See you in the next video.

