1
00:00:05,360 --> 00:00:09,840
The values method returns a view of
the dictionary's values. It's similar

2
00:00:09,840 --> 00:00:15,280
to the keys method, but instead of a
list of keys, we get a list of values.

3
00:00:15,280 --> 00:00:18,800
Once again I'll start with an example. What
I'm going to do is comment out the code we

4
00:00:18,800 --> 00:00:26,628
used in the previous lecture, and put the new code
before it. So basically, all this stuff here from d2,

5
00:00:26,628 --> 00:00:35,926
down to the bottom here. Alright. We'll come
up here. We'll put a comment about this. 

6
00:00:44,800 --> 00:00:55,200
Alright. We'll start by typing v equals d dot values
parentheses, then we'll print v. I don't recommend

7
00:00:55,200 --> 00:00:59,360
using single letter names for your variables,
by the way. It's okay to do that for small

8
00:00:59,360 --> 00:01:06,162
snippets like these, but use sensible variable
names in your programs. Okay, so let's run this,

9
00:01:06,880 --> 00:01:14,774
and we should get a dict_values
object printed out, and we can see it over here.

10
00:01:15,440 --> 00:01:20,000
And I might move that down to the bottom,

11
00:01:22,400 --> 00:01:27,360
just leaving a bit of space there for subtitles.
Alright, so a dict_values object

12
00:01:27,360 --> 00:01:31,760
looks a lot like a list, looking at that output,
and supports some of the operations that we can

13
00:01:31,760 --> 00:01:37,520
perform on lists. Just like the dict_keys
object that we saw earlier, we can't add

14
00:01:37,520 --> 00:01:43,040
new items to a dict_values object. It'll
automatically be updated if we add or remove keys,

15
00:01:43,040 --> 00:01:49,200
in the underlying dictionary. So as an
example of that, we add some code here; 

16
00:01:49,200 --> 00:02:01,810
d 10 in square brackets, equals ten in double
quotes, print parentheses v. So if we run that,

17
00:02:02,640 --> 00:02:08,281
the string value 10 appears at the end of the
dict_values when we add it to the dictionary.

18
00:02:08,281 --> 00:02:12,824
Now the documentation mentions
this, so let's switch back to my browser.

19
00:02:13,760 --> 00:02:18,000
Scroll down a bit more, until we get to
our values which we're talking about,

20
00:02:18,000 --> 00:02:23,120
and click on this documentation of
view objects link. So it says here,

21
00:02:23,120 --> 00:02:29,694
that view objects provide a dynamic view on the
dictionary's entries. If the dictionary changes,

22
00:02:29,694 --> 00:02:36,320
the changes will be seen in the keys, values and
items views. As you can see, these view objects

23
00:02:36,320 --> 00:02:42,560
support a limited subset of the operations that
we can perform with lists. One thing we can't do

24
00:02:42,560 --> 00:02:46,481
is index into them. That's a shame because
there are times when it would be useful, 

25
00:02:46,481 --> 00:02:53,200
but we can't do it. There is a reason for that, though.
I compared the keys and values objects to lists,

26
00:02:53,200 --> 00:02:58,080
but they work more like sets. We'll be looking
at sets soon, and you'll understand why we can't

27
00:02:58,080 --> 00:03:03,760
index into these objects, once you've learnt about
sets. Alright, so back to our code. Values can be

28
00:03:03,760 --> 00:03:10,984
very useful if you want to check if a value exists
in a dictionary. Let's look at some code for that.

29
00:03:11,520 --> 00:03:19,360
Line 22, print parentheses four in double
quotes, in v. And we'll do another one there.

30
00:03:19,360 --> 00:03:27,093
Next line, print parentheses double
quotes 11 in double quotes, in v -

31
00:03:27,600 --> 00:03:35,550
obviously with a space there. Let's run this,
and we can see that line 22 prints True, 

32
00:03:35,550 --> 00:03:40,720
because four is a value in the dictionary. The value
eleven isn't in the dictionary so we get False,

33
00:03:40,720 --> 00:03:46,640
printed by line 23. Sometimes you might have
to check if a value exists in your dictionary.

34
00:03:46,640 --> 00:03:50,800
Generally though, dictionaries are
intended to be accessed via their keys.

35
00:03:50,800 --> 00:03:55,040
If you find that your code is performing a lot of
checks on the values, then you should think about

36
00:03:55,040 --> 00:04:01,120
rearranging your code. A dictionary isn't efficient
for searching for values. We know that the string

37
00:04:01,120 --> 00:04:07,440
value 4 exists in our dictionary, because we got
True printed by line 22, but we've got no idea

38
00:04:07,440 --> 00:04:13,280
what its key is. There are ways around that. One
approach that you'll find online, is to convert

39
00:04:13,280 --> 00:04:20,000
both the keys and the values to lists. And the
code for that would be something like this;

40
00:04:22,400 --> 00:04:29,920
so on line 25, I start by typing keys
equals list parentheses d dot keys.

41
00:04:29,920 --> 00:04:41,840
Then on the next line, values equals list v in
parentheses, and I'll make a comment here.

42
00:04:45,520 --> 00:04:55,920
Alright then, if, and on the next line, four in double quotes, is
in values colon, and the next line, index is equal

43
00:04:55,920 --> 00:05:08,320
to values.index four in double quotes, in the
parentheses. And key equals keys and square bracket,

44
00:05:08,320 --> 00:05:12,000
index and then right square
bracket. And then we'll print

45
00:05:12,000 --> 00:05:20,080
parentheses and it's an f string double
quotes curly braces d square bracket key,

46
00:05:20,080 --> 00:05:28,720
another square bracket to close it, then our right
curly brace, was found with the key and then key in

47
00:05:28,720 --> 00:05:35,360
curly braces. Alright, so let's run the program
to check that that actually works. So we can see

48
00:05:35,360 --> 00:05:40,800
that that does work, but it's not very efficient.
We have to duplicate all the keys and values

49
00:05:40,800 --> 00:05:47,120
of the dictionary, for one thing. Lines 25 and 26
create new lists containing the keys and values

50
00:05:47,120 --> 00:05:52,880
from the dictionary. With very large dictionaries,
this will use a lot of memory. As an alternative,

51
00:05:52,880 --> 00:06:00,000
you could do the same thing by looping
through the dictionary items. Let's have a look at that.

52
00:06:01,120 --> 00:06:09,280
So put a print parenthesis on its own, on line 32, to make
a bit of space. And continuing on, for key comma value

53
00:06:09,280 --> 00:06:13,749
in d.items parentheses colon.

54
00:06:14,400 --> 00:06:22,160
Next line, if the value is equal to four
in double quotes colon print - and actually,

55
00:06:22,160 --> 00:06:27,797
we'll just take a copy of that line because it's
going to be exactly the same, save a bit of typing.

56
00:06:30,320 --> 00:06:36,080
So let's run this and see if it works, and you can see
that it has worked. It's given us the same result,

57
00:06:36,080 --> 00:06:42,320
and this time we haven't had to copy the keys and
values. It also works if the same value appears

58
00:06:42,320 --> 00:06:47,360
more than once in the dictionary. So as an
example of that, let's go back up to our dictionary.

59
00:06:47,360 --> 00:06:54,917
Up here, we're going to add another entry. I'm
going to add four, using roman numerals as the key,

60
00:06:59,040 --> 00:07:07,200
like so. Back down to our code again, and we'll
run this. As you can see, we get both of the keys

61
00:07:07,200 --> 00:07:13,200
for the value four, with that second block
of code there. The code on lines 26 to 31

62
00:07:13,200 --> 00:07:17,520
only finds the first occurrence. We'd have to do a
bit more work to find all the keys for the value four,

63
00:07:17,520 --> 00:07:22,320
using that first approach. Alright, so that's the
values method. It does have its uses, but again, if

64
00:07:22,320 --> 00:07:26,313
you find yourself using it a lot, then you should
consider if a dictionary is the most suitable

65
00:07:26,313 --> 00:07:32,514
data structure for your particular program. We'll
be looking at how dictionaries store their data soon.

66
00:07:32,514 --> 00:07:36,960
When you've seen how dictionaries work, you'll
understand why retrieving a key is significantly

67
00:07:36,960 --> 00:07:43,840
faster than getting a value. Okay, so I promised
to talk about the copy method. Let's go back.

68
00:07:46,960 --> 00:07:50,480
So specifically, I'm going to
explain about that shallow copy

69
00:07:50,480 --> 00:07:55,990
that the documentation mentions. That will also
explain the comment in the documentation of fromkeys,

70
00:07:55,990 --> 00:07:59,920
where it says that it generally doesn't
make sense for value to be a mutable object,

71
00:07:59,920 --> 00:08:06,480
such as an empty list. So we'll learn
all about that in the next few videos.

