1
00:00:05,360 --> 00:00:11,004
Let's talk about changing values in a dictionary.
Alright, so I've decided to upgrade my Virago 

2
00:00:11,004 --> 00:00:15,440
to the larger model. So let's see how to change a
value in our dictionary. Now you may have worked

3
00:00:15,440 --> 00:00:20,640
that out already - you just assign the new value to
the same key. Let's go ahead and do that, so I'm

4
00:00:20,640 --> 00:00:27,761
going to put some code down here on line 16. Put
a comment first, we're going to upgrade the Virago.

5
00:00:29,040 --> 00:00:33,760
and the actual code will be vehicles
left square bracket double quotes,

6
00:00:33,760 --> 00:00:37,200
virago, in lower case this time,
to match the key we've already got.

7
00:00:37,200 --> 00:00:44,464
equals, and we'll put in the value.
In this case, it's going to be Yamaha XV535

8
00:00:44,464 --> 00:00:48,534
closing double quote. Alright, so let's run the program.

9
00:00:49,120 --> 00:00:55,440
You can see that our Virago is now the larger,
shaft-drive version - the 535. It's not at the end

10
00:00:55,440 --> 00:01:01,268
of the output, though. You should find it on line
five, and you can see that in the output pane in my output,

11
00:01:01,268 --> 00:01:07,039
unless you're using Python 3.5 or earlier,of course,
in which case, it'll be in the output somewhere.

12
00:01:07,039 --> 00:01:12,160
Notice that the old model - the one we had
previously - is no longer present. Each key in a

13
00:01:12,160 --> 00:01:17,520
dictionary is unique, which makes sense. If the same
key could appear more than once, how would you know

14
00:01:17,520 --> 00:01:23,840
which value you were referring to. When you use the
same key again, the new value replaces the old one.

15
00:01:24,400 --> 00:01:29,120
That should answer a question you may have been
asking. What happens if you use the same key twice,

16
00:01:29,120 --> 00:01:35,802
in the literal definition of our dictionary.
So let's try that to see. We're going to come up here.

17
00:01:35,802 --> 00:01:40,560
What I'm going to do is add a new
entry on line 10: single quote roadster

18
00:01:42,720 --> 00:01:46,240
closing single quote colon,
then another single quote,

19
00:01:46,240 --> 00:01:49,772
and we're going to add the
value, which is Triumph

20
00:01:50,880 --> 00:01:57,760
Street Triple, single quote again to
close that off, and a comma. Now, if we run the

21
00:01:57,760 --> 00:02:04,349
program again now, you can see that we've
only got one roadster in the output. 

22
00:02:04,349 --> 00:02:09,770
Once again, if you're running Python 3.5 or earlier,
the output will be in a random order.

23
00:02:09,770 --> 00:02:15,920
I'm using Python 3.8, and that preserves the
insertion order of dictionary keys, and in fact,

24
00:02:15,920 --> 00:02:22,320
Python 3.7 does the same. Note that it preserves
the insertion order - emphasis on insertion.

25
00:02:22,320 --> 00:02:27,944
If you expected the roadster to appear after
the fiesta, that should now explain why it doesn't. 

26
00:02:27,944 --> 00:02:32,820
Our roadster was inserted as the second
entry in the dictionary - that's on line three.

27
00:02:35,520 --> 00:02:40,240
We then replaced the value with the Street
Triple, on line 10. The keys don't get re-ordered

28
00:02:40,240 --> 00:02:49,291
when you do that. The insertion order of the keys is preserved.
Notice that the virago is still at position 5 in our output.

29
00:02:51,456 --> 00:02:56,329
Although we made a change to the value on line 18,

30
00:02:56,329 --> 00:03:00,480
that doesn't cause the key to be moved.
Also note that if I go back to

31
00:03:00,480 --> 00:03:06,640
our dictionary, we defined at the top, there's a
warning there from IntelliJ on lines 3 and 10:

32
00:03:06,640 --> 00:03:11,920
Dictionary contains duplicate keys 'roadster'.
You wouldn't normally use the same key twice 

33
00:03:11,920 --> 00:03:17,303
in a dictionary literal. It's just a waste of time and
typing. The last value will replace any earlier ones.

34
00:03:17,303 --> 00:03:21,680
So what I'm going to do is come up here to
line three. Click on that and use the light bulb

35
00:03:21,680 --> 00:03:27,627
to delete that entry, or remove this key.
You can see that's now disappeared. 

36
00:03:27,627 --> 00:03:32,400
Alright, so we'll see plenty of examples of adding
and changing keys. In the next video, I want to

37
00:03:32,400 --> 00:03:37,920
show you a couple of ways to delete an entry
from a dictionary. See you in the next video.

