1
00:00:05,360 --> 00:00:09,870
In this video, we'll look at what the documentation
means when it talks about a shallow copy. 

2
00:00:09,870 --> 00:00:17,848
I'm going to create a new Python file for these examples. I'm
going to call mine shallow copy underscore py.

3
00:00:22,960 --> 00:00:27,571
Alright, so we need a dictionary to
work with. Let's create just a simple one;

4
00:00:27,571 --> 00:00:37,071
animals equals, left and right curly braces and
we'll start with a lion as the key. Value is scary.

5
00:00:38,480 --> 00:00:50,354
elephant, value, big. Last one teddy, is
the key, colon, cuddly  as the value.

6
00:00:50,880 --> 00:00:56,960
Alright. Before we start working with copies,
let's review references to mutable objects; things

7
00:00:56,960 --> 00:01:04,655
equals animals. On the next line, animals
square brackets double quotes, teddy,

8
00:01:05,120 --> 00:01:09,200
closing right square brackets,
equals toy in double quotes,

9
00:01:09,200 --> 00:01:16,080
and print parentheses things square
brackets, teddy in double quotes,

10
00:01:16,080 --> 00:01:22,080
and it's closing square brackets and closing
parentheses. Alright, so pause the video, 

11
00:01:22,080 --> 00:01:26,320
before running this next bit of code, and work out what
the output will be. And if you're not sure what

12
00:01:26,320 --> 00:01:31,040
will be printed, review the lecture Binding
multiple names to a list in the Lists and

13
00:01:31,040 --> 00:01:36,657
Tuples section of this course. So pause the video
now and come back when you've got your answer.

14
00:01:37,680 --> 00:01:39,840
Alright, so welcome back - let's run this.

15
00:01:40,960 --> 00:01:46,400
Okay, so there's a result - both animals and things
are referring to the same dictionary. If we change

16
00:01:46,400 --> 00:01:50,480
a value in the dictionary, we get the new value
printed. It doesn't matter which reference we

17
00:01:50,480 --> 00:01:56,240
use to change the dictionary - there is only one
dictionary, and it gets updated on line eight. 

18
00:01:56,240 --> 00:02:02,400
And we saw that when I ran the program. Now if we want
two separate dictionaries, we should create a copy.

19
00:02:02,400 --> 00:02:10,479
I'm going to change line seven to do that. Instead
of things equals animals, put animals.copy.

20
00:02:10,479 --> 00:02:15,440
We now have two separate dictionaries. We
modify the animals dictionary on line eight,

21
00:02:15,440 --> 00:02:22,240
but things isn't affected. So I run the
program - let's confirm that is the case.

22
00:02:22,240 --> 00:02:28,400
We can see now the entry for teddy in the things
dictionary is still cuddly. And continuing on to

23
00:02:28,400 --> 00:02:36,975
that on the next line, we can do a print parentheses
animals square brackets, in double quotes, teddy,

24
00:02:37,760 --> 00:02:42,344
then square brackets and closing
off the parentheses. If we run that,

25
00:02:44,320 --> 00:02:48,349
we can see that the teddy entry
from animals, now has the value toy.

26
00:02:48,349 --> 00:02:53,200
so we use the copy method, on line 7, to
create a copy of the original dictionary.

27
00:02:53,200 --> 00:02:58,480
Any changes we make to animals isn't reflected
in the things dictionary, and that's because now,

28
00:02:58,480 --> 00:03:04,560
things is a new dictionary created by copying
animals. That's fairly straightforward but what's

29
00:03:04,560 --> 00:03:09,840
all this shallow copy about? With immutable
values, it makes no difference whether a copy

30
00:03:09,840 --> 00:03:15,840
is shallow or deep. Where it becomes important is
when you have mutable values in the dictionary.

31
00:03:15,840 --> 00:03:20,869
So let's change our values to be lists instead
of strings. So coming back up to our dictionary,

32
00:03:20,869 --> 00:03:28,050
for lion, we're going to enclose that in square
brackets. I'm going to add a few entries here.

33
00:03:31,040 --> 00:03:36,000
Alright, and do the same for the next two lines as well;

34
00:03:37,280 --> 00:03:39,520
grey, wrinkled,

35
00:03:41,600 --> 00:03:48,327
three entries. And for the last one,
the teddy, we'll do the same; cuddly

36
00:03:49,760 --> 00:03:56,579
stuffed. Okay. Each of our values
in our dictionary are now lists -

37
00:03:57,600 --> 00:04:02,003
I'm going to delete this entry here, line 8. 

38
00:04:03,200 --> 00:04:05,565
When we run this now,

39
00:04:06,480 --> 00:04:12,729
we get the same list printed; the teddy key in
each dictionary. Now I emphasize the word same here,

40
00:04:12,729 --> 00:04:18,000
because they really are the same list. We performed
a shallow copy and we get two dictionaries,

41
00:04:18,000 --> 00:04:23,456
but there are only three lists, not the six lists
that you may have expected. So to demonstrate that,

42
00:04:23,456 --> 00:04:28,733
what I'm going to do is mutate the
entry for teddy in the things dictionary.

43
00:04:31,680 --> 00:04:35,090
So we'll do a print parentheses
to separate the output;

44
00:04:35,090 --> 00:04:40,425
on line 14, things teddy in
square brackets and double quotes,

45
00:04:41,600 --> 00:04:51,000
dot append, and in parentheses, toy in double
quotes. Let's go up and copy these two lines

46
00:04:52,947 --> 00:04:57,709
to print those values out again. Alright, so what
I've done here is, I've appended another item

47
00:04:57,709 --> 00:05:02,640
to teddy's list in the things directory.
We've added toy to the list, on line 14.

48
00:05:02,640 --> 00:05:06,834
Run the program now, and we'll see if
the output is what you expected.

49
00:05:07,760 --> 00:05:13,760
As you can see, the new item toy has appeared
both times. Now some students will be quite

50
00:05:13,760 --> 00:05:18,800
happy with what happened here. Others - probably
most - will be totally confused. If you're totally

51
00:05:18,800 --> 00:05:24,880
confused, don't worry - this is confusing. At some
point, you'll have a eureka moment, and it will

52
00:05:24,880 --> 00:05:28,800
all suddenly make perfect sense. It really is
worth putting in the effort to understand all

53
00:05:28,800 --> 00:05:33,200
this because it's fundamental to many things
that happen, when programming. I'll use the next

54
00:05:33,200 --> 00:05:39,033
couple of videos to explain it in more detail,
and in different ways. See you in the next video.

