1
00:00:05,280 --> 00:00:10,400
in this video we'll take a look at the remaining
dict methods - the ones we haven't used yet. We're

2
00:00:10,400 --> 00:00:14,320
not going to write complete programs that
use them. We'll use some simple examples,

3
00:00:14,320 --> 00:00:18,320
to demonstrate their behavior. The methods
can be very useful when you need them,

4
00:00:18,320 --> 00:00:22,560
but aren't as commonly used as the methods
we've already covered. I'm going to create a

5
00:00:22,560 --> 00:00:28,422
new python file for these examples, I'm
going to call this one dict_methods.py

6
00:00:34,480 --> 00:00:37,040
Alright, so we're going to need a
dictionary to work with, and a simple

7
00:00:37,040 --> 00:00:41,120
one will best demonstrate the effects of these
methods. I'm going to paste in a dictionary,

8
00:00:41,120 --> 00:00:46,400
and a list. You can copy these definitions from
the file d.text, in the resources for this video,

9
00:00:47,840 --> 00:00:52,480
and you can see them on screen there. Alright, 
I'm going to start with the fromkeys class method,

10
00:00:53,040 --> 00:00:56,560
in the documentation. Now the Python
dictionary class is called dict,

11
00:00:56,560 --> 00:01:02,080
and up until now we've referred to it as a type.
When the documentation talks about a class method,

12
00:01:02,080 --> 00:01:06,000
it means you can use the class name to call
the method. So let's see how that works:

13
00:01:06,800 --> 00:01:16,058
so we're going to type new_dict is equal to
dict.fromkeys and, in parentheses, pantry_items,

14
00:01:16,640 --> 00:01:23,840
and we'll print out the new dictionary. On line
16 we use dict.fromkeys to call the method.

15
00:01:23,840 --> 00:01:29,360
The dictionary class is called dict, and we're
using that class to call the fromkeys method.

16
00:01:29,360 --> 00:01:34,320
We're passing an iterable to the method.
I've used the pantry_items list from line 14.

17
00:01:35,200 --> 00:01:39,520
Alright, so let's run the program, and check the
output. So we've got a dictionary with five keys,

18
00:01:39,520 --> 00:01:45,680
all having the value None. The keys came from the
iterable that we passed to the from_keys method.

19
00:01:45,680 --> 00:01:51,360
Each key is one of the items in the pantry_items
list. Now, we can change that default value,

20
00:01:51,360 --> 00:01:57,200
which is currently None, by passing another value
to the method. Come up here, put a comma zero.

21
00:01:58,880 --> 00:02:06,720
Run that, this time all the values are zero. so
this fromkeys method is a useful way to create

22
00:02:06,720 --> 00:02:12,480
an initial dictionary, which your code will then
change. Each key starts off with the default value

23
00:02:12,480 --> 00:02:17,520
0, in this case. If you were creating an empty
pantry, for example, this could be a good way to

24
00:02:17,520 --> 00:02:22,000
do it. The code would then change the amounts,
when the cook entered how much they had of each

25
00:02:22,000 --> 00:02:27,120
item. Right, so I'm going to comment out those two
lines, and we'll move on now to the keys method.

26
00:02:28,880 --> 00:02:33,120
I'm going to demonstrate what it does, but I can't
give many useful examples of when you'd use it.

27
00:02:33,680 --> 00:02:38,560
It's really a legacy from Python 2, where it was
needed for iterating over a dictionary's keys.

28
00:02:39,440 --> 00:02:44,320
We'll type keys equals d dot keys parentheses,
then we'll print out keys on the next line.

29
00:02:46,400 --> 00:02:53,920
If we run this, the output looks a bit like a
list. When the keys method was added to Python 2.2

30
00:02:53,920 --> 00:02:59,360
it did return a list - a list containing all
the dictionary keys. That's not very efficient,

31
00:02:59,360 --> 00:03:03,920
because it involved copying the keys, which
could be a problem with very large dictionaries.

32
00:03:03,920 --> 00:03:09,200
In Python 3, the keys method produces
a view object. It's similar to a list,

33
00:03:09,200 --> 00:03:14,400
but you can't add or remove items from it. If you
add or remove keys to the underlying dictionary,

34
00:03:14,400 --> 00:03:20,000
the value will be updated automatically. There
are very few situations where you have to use this

35
00:03:20,000 --> 00:03:25,600
keys method, in Python 3. Some programmers use it
because it makes the meaning of the code explicit;

36
00:03:26,800 --> 00:03:28,124
let's look at an example

37
00:03:30,800 --> 00:03:36,480
So on line 22 I'm going to type for item
in d colon then print item in parentheses.

38
00:03:38,080 --> 00:03:42,640
So it's not very obvious what's happening here.
We can see that we're iterating over something,

39
00:03:42,640 --> 00:03:46,560
but there's no indication of what that
something is. If we run the program,

40
00:03:48,160 --> 00:03:52,560
we can see the dictionary keys printed out.
Now, some programmers would actually change that

41
00:03:53,120 --> 00:04:02,400
to be d dot keys and parentheses. If I run
that again, we get exactly the same results,

42
00:04:03,440 --> 00:04:08,800
but it's now clearer what we're iterating over,
on line 22. When you see dot keys, you know you

43
00:04:08,800 --> 00:04:13,520
must be dealing with a dictionary. So that could
be a good way to make the code easier to read

44
00:04:13,520 --> 00:04:18,320
in a large program. As I said, though, it's
rare that you must use the keys method.

45
00:04:18,320 --> 00:04:22,480
The only situation I can think of, where you'd
have to use it, is if you want to perform certain

46
00:04:23,036 --> 00:04:28,396
set operations on the keys of a dictionary. We'll
come back to that, when we've learnt about sets.

47
00:04:28,880 --> 00:04:32,880
Alright, so that leaves the update and
values methods. We'll look at those next,

48
00:04:32,880 --> 00:04:37,600
then I'll discuss the copy method that I've
skipped over. See you in the next video.

