1
00:00:05,280 --> 00:00:09,360
Alright, so you should now be comfortable
returning values from a dictionary, using

2
00:00:09,360 --> 00:00:14,800
their keys. Let's see a few more things we can
do with dictionaries. Although they're not a

3
00:00:14,800 --> 00:00:21,040
sequence type, they are iterable. We can iterate
over the keys in a dictionary. Now the syntax

4
00:00:21,040 --> 00:00:25,440
is the same as we've used before, so let's have
a look at the code. So what I'm going to do is

5
00:00:25,440 --> 00:00:31,920
delete the code from the last video, and just keep
the dictionary definition. Alright, so I'm going

6
00:00:31,920 --> 00:00:39,840
to start by typing for key in vehicles colon,
and print key in parentheses on the next line.

7
00:00:42,640 --> 00:00:43,840
Alright, so if we run this now,

8
00:00:49,200 --> 00:00:52,320
you can see there, that we've got
the dictionary keys printed out.

9
00:00:52,320 --> 00:00:58,400
So when you iterate over a dictionary, you're
iterating over the keys. If you want the values,

10
00:00:58,400 --> 00:01:04,160
you might decide to index the dictionary. I'll do
that to print out the values, as well as the keys.

11
00:01:04,160 --> 00:01:10,560
So I'm going to change line 13, specifically,
and put comma after the key and vehicles,

12
00:01:11,440 --> 00:01:17,200
then left square bracket, key. So key
in square brackets, in other words.

13
00:01:18,547 --> 00:01:25,175
Alright, so if we run this now, you can see
we've got the key this time, followed by the value.

14
00:01:25,175 --> 00:01:31,704
But it's not obvious which is which here,
but we can fix that by using f strings to format the output better. 

15
00:01:31,704 --> 00:01:34,880
We'll get plenty of practice
doing that, so I'll do something different here.

16
00:01:35,600 --> 00:01:40,720
If you recall, we saw the sep argument that we can
pass to the print function, back in section five,

17
00:01:40,720 --> 00:01:46,640
but you may have forgotten about it by now. So what
I'm going to do is use it here, as a reminder - 

18
00:01:46,640 --> 00:01:52,085
just as a reminder that it's there if you need it. What
I'm going to do after the right square bracket there,

19
00:01:52,085 --> 00:02:00,000
is put comma sep equals, and in double quotes,
a comma and a space. Run the program again now.

20
00:02:01,120 --> 00:02:06,480
This time you can see that we've got the comma
and a space between the key and the value itself.

21
00:02:07,600 --> 00:02:13,360
So it's more efficient to use the items of a
dictionary instead. Back in the Lists, Ranges

22
00:02:13,360 --> 00:02:19,520
and Tuples section, we talked about the enumerate
function. That provides an efficient way to get the

23
00:02:19,520 --> 00:02:25,760
indexes and items from a sequence. If you remember,
we unpacked the index and value from the tuples

24
00:02:25,760 --> 00:02:31,200
that enumerate returns. If you don't remember,
review the lecture called The enumerate Function

25
00:02:31,200 --> 00:02:36,800
in that section, and the  Improving our Code lecture
that followed it. Now we can do something similar

26
00:02:36,800 --> 00:02:42,480
with dictionaries. We don't have to call enumerate,
because dictionaries have the dot items method

27
00:02:42,480 --> 00:02:47,920
that we can use instead. So let's change that
code to do the same thing more efficiently.

28
00:02:47,920 --> 00:02:52,171
So what I'm going to do is comment out
the current loop and just add a new one.

29
00:02:55,600 --> 00:02:59,040
Alright, so I'm going to type for key comma value

30
00:03:00,000 --> 00:03:08,085
in vehicles dot items, and you can see there with
parentheses on the end, colon. Then we're going to print,

31
00:03:08,085 --> 00:03:15,330
and in parentheses, key comma value comma
sep equals and in double quotes, comma space.

32
00:03:17,200 --> 00:03:23,769
So if we run the program now, you can see
we've got the same output as we got previously.

33
00:03:25,120 --> 00:03:28,480
So students sometimes object when we
show code that you shouldn't write.

34
00:03:29,120 --> 00:03:34,560
We've done it because the loop on lines 12 and
13 - the loop that I've now commented out - is the

35
00:03:34,560 --> 00:03:39,680
way most new programmers would try to do this. It
works, so they develop the habit of writing their

36
00:03:39,680 --> 00:03:45,120
code like that. Now it's not bad, but it's less
efficient than iterating over items like we're

37
00:03:45,120 --> 00:03:52,658
doing, on lines 14 and 15. With larger data
sets, those few milliseconds can soon add up.

38
00:03:53,840 --> 00:03:57,280
you might come across that inefficient
loop if you find yourself modifying

39
00:03:57,280 --> 00:04:04,160
or converting old python 2 code. dot items in
python 2 created a list. That means it needed a

40
00:04:04,160 --> 00:04:09,920
copy of the data, and used more memory. For that
reason, programs might have used the simple for

41
00:04:09,920 --> 00:04:18,320
loop to avoid the extra memory overhead of using
dot items. Python 2.2 added a dot iteritems method

42
00:04:18,320 --> 00:04:24,560
which works in a similar way to python 3's dot
items method. Python 3 now uses something called

43
00:04:24,560 --> 00:04:30,960
a generator, and doesn't copy the data from the
dictionary. With python 3, remember to use enumerate

44
00:04:30,960 --> 00:04:36,739
when iterating over sequences, and dot items when
iterating over dictionaries.

45
00:04:37,276 --> 00:04:44,560
Alright, so the dot items method returns an iterator of tuples,
and we unpack the tuples into our key and value variables,

46
00:04:44,560 --> 00:04:51,120
on line 14. Alright, so that's how we can iterate
over the keys and items of a dictionary. 

47
00:04:51,120 --> 00:04:56,351
I'll finish the video with a bit of history, because
some students might be getting strange results.

48
00:04:57,520 --> 00:05:02,560
If you installed Python from the python.org
website, the implementation of Python that you

49
00:05:02,560 --> 00:05:08,400
installed from that site is called CPython. That's
because most of the implementation is written in

50
00:05:08,400 --> 00:05:14,480
C. There are other implementations of Python,
for example Iron Python, or IPython, and Jython.

51
00:05:16,960 --> 00:05:22,080
CPython is the reference implementation
of Python, the version written by Guido

52
00:05:22,080 --> 00:05:27,920
van Rossum. Version 3.6 of CPython
changed the way that dictionaries worked.

53
00:05:27,920 --> 00:05:32,320
In earlier versions, the keys of a dictionary
were unordered. That means you could get the

54
00:05:32,320 --> 00:05:38,320
keys printed in a different order each time
you ran the program. If you're using python 3.5,

55
00:05:38,320 --> 00:05:42,000
and the dictionary prints in a different
order each time, now you know why that is.

56
00:05:43,920 --> 00:05:49,200
Python 3.6 preserved the insertion order
of dictionary keys. That means you'll always

57
00:05:49,200 --> 00:05:54,720
iterate over them in the same order. That was an
implementation detail, which meant you couldn't

58
00:05:54,720 --> 00:06:02,000
rely on the behavior in other implementations
of python 3.6. With python 3.7, that behavior

59
00:06:02,000 --> 00:06:07,200
became part of the language. When you iterate
over a dictionary, with python 3.7 and above,

60
00:06:07,200 --> 00:06:13,120
the keys will appear in the order they were added
to the dictionary. Now that's something you need

61
00:06:13,120 --> 00:06:19,040
to be aware of, because you can get a different
order in earlier versions of Python. Python 3.5

62
00:06:19,040 --> 00:06:24,160
goes out of support in September 2020 but that
doesn't mean people will stop using it. But it

63
00:06:24,160 --> 00:06:33,112
does mean it's acceptable for you to specify that your
code requires pythons 3.6 or higher, if you rely on
features introduced in that version.

64
00:06:33,335 --> 00:06:37,920
Alrighty, so let's move on now to the next video.

