1
00:00:05,200 --> 00:00:07,440
In this video, we'll have a
look at the update method.

2
00:00:08,160 --> 00:00:11,600
It's used to update one dictionary
with the contents of another,

3
00:00:11,600 --> 00:00:15,440
and works pretty much how you'd expect.
But you have to remember that dictionary

4
00:00:15,440 --> 00:00:19,600
keys are unique. The same key can't
appear more than once in a dictionary.

5
00:00:20,240 --> 00:00:23,920
So with that in mind, the behaviour
of update makes perfect sense.

6
00:00:23,920 --> 00:00:27,840
So let's see an example. I'll create a new
dictionary, and we'll use that to update d.

7
00:00:29,360 --> 00:00:31,840
Alright, so I'm going to leave
a comment here, this old code,

8
00:00:32,400 --> 00:00:43,840
the earlier code, and then we'll add the new code

9
00:00:58,240 --> 00:00:59,760
Alright, so I'm going to
comment the rest of this out,

10
00:01:03,680 --> 00:01:08,640
and up here, on line 16, we're
going to create a new dictionary.

11
00:01:08,640 --> 00:01:13,280
So d2 is equal to left right curly braces 7 colon

12
00:01:16,240 --> 00:01:25,840
lucky seven double quotes comma ten colon ten
in double quotes comma and 3 colon double quotes

13
00:01:26,720 --> 00:01:32,160
this is the new three, double quotes comma,
then we've got our ending right curly brace.

14
00:01:32,800 --> 00:01:35,840
Now I've deliberately mixed up
the ordering of this dictionary,

15
00:01:35,840 --> 00:01:40,080
to check the effect on the existing
keys in the dictionary being updated.

16
00:01:40,080 --> 00:01:43,760
Alright, so let's update our original
dictionary d, this one up here, on line 1.

17
00:01:45,920 --> 00:01:50,080
So I'm going to type d dot
update d2 in parentheses.

18
00:01:51,120 --> 00:01:57,120
Then we'll add some more code here, for key
comma value in d dot items parentheses colon.

19
00:01:57,920 --> 00:02:01,440
We'll print out the key in
value key value in parenthesis.

20
00:02:01,440 --> 00:02:10,080
Let's run this, to check out the results.
Put in that format, so it's a bit easier to read.

21
00:02:12,240 --> 00:02:15,920
Alright, so there's the output.
So what we've done here, is we've updated the

22
00:02:15,920 --> 00:02:22,640
values for two keys, and inserted one new value.
So the value for key 3 has been changed,

23
00:02:22,640 --> 00:02:27,360
but notice that 3 is still in the same place
- it's the fourth item in the dictionary.

24
00:02:27,360 --> 00:02:29,600
Updating the value doesn't
change the position of the key.

25
00:02:30,160 --> 00:02:34,400
That's the same behaviour we got when we
changed the value of virago in our vehicles

26
00:02:34,400 --> 00:02:38,480
dictionary, in the previous video.
We can see the same thing for 7;

27
00:02:38,480 --> 00:02:43,280
the value's changed to lucky seven, but
the key remains in its original position.

28
00:02:43,280 --> 00:02:46,560
Updating the value of a key doesn't
affect its position in the dictionary.

29
00:02:47,120 --> 00:02:51,440
Once again, that's what the documentation
means by the insertion order being preserved.

30
00:02:52,240 --> 00:02:57,200
We also added a new key, 10, to the dictionary,
and we can see that got inserted at the end.

31
00:02:58,160 --> 00:03:01,840
The documentation mentions that we
can pass a dictionary to update,

32
00:03:01,840 --> 00:03:03,360
which is what we've just done.

33
00:03:03,360 --> 00:03:07,120
It also mentions passing an iterable
of key value pairs, as tuples.

34
00:03:08,160 --> 00:03:11,920
We know of a way to create an
iterable of key value pairs as tuples.

35
00:03:11,920 --> 00:03:15,200
The enumerate function does that,
so let's have a go at doing that.

36
00:03:17,360 --> 00:03:17,860
So down here

37
00:03:21,560 --> 00:03:28,400
line 26, I'm just going to do an empty print
here, so print parentheses. Move things up a bit.

38
00:03:31,040 --> 00:03:37,520
Then I'm going to type d dot update, and in
parentheses, enumerate parentheses pantry

39
00:03:38,320 --> 00:03:46,160
underscore items and closing right parentheses.
Then we'll do the same thing, for key comma value

40
00:03:46,160 --> 00:03:52,240
in d dot items again parentheses colon,
and we'll print out key comma value again.

41
00:03:54,240 --> 00:03:58,240
Let's run the program again, and we're
looking at that second block of output.

42
00:03:58,240 --> 00:04:01,520
The first five items have
been replaced with the values

43
00:04:01,520 --> 00:04:06,160
from our pantry underscore items list.
Okay, so that's the update method.

44
00:04:06,160 --> 00:04:11,040
It takes the key value pairs that we passed
to it, and uses them to update a dictionary.

45
00:04:11,040 --> 00:04:15,360
Any existing keys have their values
replaced by the values we passed to update.

46
00:04:16,240 --> 00:04:19,920
Before we move on, I'll quickly mention
another way to update a dictionary.

47
00:04:20,720 --> 00:04:29,360
Switching back to the documentation, and scrolling
down to the update method, just down here.

48
00:04:30,880 --> 00:04:34,800
So just below the update method there's two
new operators, that were added to python 3.9.

49
00:04:35,360 --> 00:04:38,480
These are the union operator, the
pipe character you can see there,

50
00:04:38,480 --> 00:04:43,200
and the augmented union assignment operator.
That's the pipe character and the equal sign.

51
00:04:43,760 --> 00:04:47,440
If you check the description of the update method,
I'll just go up a little bit so you can see it,

52
00:04:47,440 --> 00:04:54,960
and if we compare that to the augmented union
assignment operator, you'll see that they're

53
00:04:54,960 --> 00:04:58,480
actually doing the same thing. Just go up a
little bit more, so we can hopefully see both.

54
00:05:00,640 --> 00:05:04,480
So there's the update again. Update the
dictionary with the key value pairs from other,

55
00:05:04,480 --> 00:05:10,880
overriding existing keys. You can see we've
got essentially the same thing in the augmented

56
00:05:10,880 --> 00:05:14,000
union assignment operator. So here you
can clearly see the descriptions are

57
00:05:14,000 --> 00:05:18,400
written slightly differently, but both update
a dictionary with keys and values from other.

58
00:05:18,400 --> 00:05:23,360
As you can see. The update function will also
accept keyword arguments which is, something you

59
00:05:23,360 --> 00:05:28,560
can do with a function but not with an operator.
So I suggest you stick to using the update method,

60
00:05:28,560 --> 00:05:35,200
and ignore this augmented union assignment.
So that's that d pipe equals other,

61
00:05:35,200 --> 00:05:38,960
just below the middle of the screen.
If you use either of these new operators,

62
00:05:38,960 --> 00:05:42,880
your code won't run on Python
versions before Python 3.9.

63
00:05:42,880 --> 00:05:46,480
So let's have a quick look at how long
those earlier versions will be used for.

64
00:05:47,280 --> 00:05:54,080
I've got a page here, which I want to go to. This
page has the release dates and end of life dates,

65
00:05:54,080 --> 00:05:59,120
in a table, which can be really useful to actually
know if you're using a specific version of Python.

66
00:05:59,120 --> 00:06:06,560
As you can see, Python 3.8 doesn't reach end
of life until October 2024. If you write code

67
00:06:06,560 --> 00:06:11,600
that uses these new assignment operators,
over here, your code would be unavailable

68
00:06:11,600 --> 00:06:16,000
to a lot of Python users, for at least four
years from the date I've recorded this video.

69
00:06:16,560 --> 00:06:20,560
Ultimately, it's your choice, of course, but we
certainly won't be using them in this course.

70
00:06:21,200 --> 00:06:24,880
Alright, so moving on, in the next
video we'll look at the values method

71
00:06:24,880 --> 00:06:26,736
and see how it can be useful.

72
00:06:26,736 --> 00:06:28,400
See you in the next video.

