1
00:00:05,440 --> 00:00:09,384
Alright, so we've seen one way to create
a dictionary in python, using a literal.

2
00:00:09,384 --> 00:00:14,880
So that's the code, you can see on screen on lines one
through ten. We've also seen how to iterate over a

3
00:00:14,880 --> 00:00:20,640
dictionary. In fact, we've seen two ways to do that,
on lines 12 and 13, which is now commented out,

4
00:00:20,640 --> 00:00:27,280
but also on lines 14 and 15. In this video, we'll add
some more entries to our dictionary. Now personally,

5
00:00:27,280 --> 00:00:31,920
I've always wanted my own private jet, so let's add
one of those. I'm going to put the code I'm about to

6
00:00:31,920 --> 00:00:36,800
type in, before we print out the dictionary, to save
printing it twice. so specifically, I'm going to

7
00:00:36,800 --> 00:00:44,400
put it down here, on line 12. So I'm going to type
vehicles square bracket double quotes starfighter,

8
00:00:46,560 --> 00:00:51,840
closing off the double quotes and a right square
bracket, equals and then double quote Lockheed

9
00:00:54,480 --> 00:01:00,640
F-104 closing double quote there. Notice that
dictionaries don't have an append method.

10
00:01:01,200 --> 00:01:06,960
We assign the value to the dictionary, using
it's key. Let's confirm it works by running it.

11
00:01:08,320 --> 00:01:12,320
We can see down the bottom now we've
got starfighter Lockheed F-104.

12
00:01:13,600 --> 00:01:20,080
Now if you're using python 3.5 or earlier, the
starfighter might not be the last entry, 

13
00:01:20,080 --> 00:01:25,520
but it should be in there somewhere. Alright,
so on line 12 we use the key starfighter,

14
00:01:25,520 --> 00:01:32,720
as an index into the dictionary. We assign the
value Lockheed F-104 to it. The starfighter looks

15
00:01:32,720 --> 00:01:37,440
pretty cool, like something from thunderbirds,
but that's not a good reason to buy an aeroplane.

16
00:01:37,440 --> 00:01:42,952
Perhaps I should choose something more suitable
for flying to conferences. How about a Learjet? 

17
00:01:42,952 --> 00:01:48,160
Let's add a Learjet, vehicles square bracket,
or brackets, double quotes, this time Learjet.

18
00:01:50,880 --> 00:01:53,840
That's our key, and then equals
outside of the square brackets,

19
00:01:54,800 --> 00:02:01,200
bombardier Learjet, should've
done a capital B there, and Learjet.

20
00:02:02,960 --> 00:02:10,160
75, closing double quote there. If we
run the program, we've got Learjet

21
00:02:10,160 --> 00:02:15,280
showing in the last line of the output. Alright,
so let's see one more example before we move on.

22
00:02:17,440 --> 00:02:22,160
time to come back to earth. The only plane
I'm likely to fly is one of these. Alright,

23
00:02:22,160 --> 00:02:27,360
so it's time for a challenge. Add the code
to insert the value glider to our dictionary,

24
00:02:27,360 --> 00:02:31,040
with the key toy. Pause the video and
come back when you've made the change.

25
00:02:34,240 --> 00:02:37,680
Alright, so adding the glider is
the same as the other two planes.

26
00:02:38,720 --> 00:02:43,360
We'll start on line 14: vehicles
square brackets double quote,

27
00:02:43,360 --> 00:02:50,640
toy being the key, and outside of the right
square bracket, equals glider in double quotes.

28
00:02:53,680 --> 00:02:59,040
Run the program again, and we see that's been
added. Now notice that all our new entries are

29
00:02:59,040 --> 00:03:04,000
appearing at the end of the list. The first
eight entries are in the order we typed them.

30
00:03:04,720 --> 00:03:10,400
They're the ones on lines one to nine,
and that's our literal values that we typed on

31
00:03:10,400 --> 00:03:18,374
lines one to nine. Next we added the starfighter, the learjet
and the toy, and that's the order we get them printed out.

32
00:03:18,374 --> 00:03:22,480
So that's what the documentation means
when it says that dictionaries preserve

33
00:03:22,480 --> 00:03:28,320
the insertion order. As I've mentioned, if
you're using Python version 3.5 or earlier

34
00:03:28,320 --> 00:03:34,400
you'll get the entries appearing in random order.
Prior to Python 3.6, dictionaries were unordered.

35
00:03:34,400 --> 00:03:38,800
You couldn't rely on the order of the keys, when
iterating over the dictionary, in those versions.

36
00:03:38,800 --> 00:03:44,800
CPython 3.6 preserved the insertion order, and
that became a language feature with Python 3.7

37
00:03:45,360 --> 00:03:49,520
In a later video, we'll see how to iterate
over the keys in order. Now that can be

38
00:03:49,520 --> 00:03:54,720
useful, because inserting and deleting keys can
result in a sorted dictionary becoming unsorted.

39
00:03:55,321 --> 00:04:01,734
But first though, in the next video, we'll look at changing
the value associated with a key, and also deleting keys.

40
00:04:01,734 --> 00:04:06,240
See you in the next video.

