1
00:00:05,360 --> 00:00:09,840
Okay, so let's clear up some confusion that you
may be feeling. I'm going to work with a copy of

2
00:00:09,840 --> 00:00:15,831
our shallow_copy code. So we're going
to right click on that file in our project pane,

3
00:00:15,831 --> 00:00:22,390
and choose Copy, Copy. I'm going to right
click the project up here and choose Paste.

4
00:00:22,720 --> 00:00:26,853
I'm going to call the new file
shallow_copy_2

5
00:00:27,760 --> 00:00:32,960
Close the project pane. Now when you make a
copy of a file like this it's a good idea

6
00:00:32,960 --> 00:00:37,808
to close the original. This makes it less
likely that you'll work with the wrong file.

7
00:00:37,808 --> 00:00:42,720
IntelliJ has a neat feature to help with that, so
right click on the editor tab for the new file,

8
00:00:42,720 --> 00:00:48,720
and click on Close Others. That closes all files, as
you saw, except the one that you're working with.

9
00:00:48,720 --> 00:00:52,320
Alright, so next, we'll have a look at
what's going on with our dictionaries,

10
00:00:52,320 --> 00:00:58,400
and what a shallow copy is actually doing. I'm
going to rearrange the animals dictionary slightly,

11
00:00:58,400 --> 00:01:04,080
to reflect what Python's doing behind the scenes.
Now we've typed it as a literal value, but that's

12
00:01:04,080 --> 00:01:09,611
not really how it's stored. Looking at line
two, the value for the key lion, is a list.

13
00:01:09,611 --> 00:01:13,600
The way we've typed it, it looks like
the list appears inside the dictionary,

14
00:01:13,600 --> 00:01:18,282
but that's not what's really happening. Instead,
the list is created in memory somewhere,

15
00:01:18,282 --> 00:01:22,380
and the value is a reference to it. It really
looks more like this: what I'm going to do

16
00:01:22,380 --> 00:01:33,092
is start typing here, lion_list
equals, and we're gonna copy that list, paste it,

17
00:01:35,360 --> 00:01:37,767
and replace it in here.

18
00:01:38,640 --> 00:01:43,985
So line one we've got a list,
inside the dictionary on line four.

19
00:01:43,985 --> 00:01:50,392
The value for the key lion, is that
list. So do the same thing for the other two lists.

20
00:02:16,480 --> 00:02:20,092
Okay. Alright, so that's
a more accurate reflection

21
00:02:20,092 --> 00:02:26,652
of how our dictionary's really stored. The values
on lines 6, 7 and 8 are references to the lists.

22
00:02:26,652 --> 00:02:33,000
When we perform a shallow copy, like we're doing
on line 11, the references are copied into the new dictionary.

23
00:02:33,000 --> 00:02:38,669
Before we have a closer look at what's
happening on line 18, I'll run the program again.

24
00:02:39,920 --> 00:02:47,173
The teddy value in both dictionaries now contains
the extra item, toy. Because both dictionaries

25
00:02:48,080 --> 00:02:51,999
contain references to the same lists,
that really shouldn't be surprising.

26
00:02:51,999 --> 00:02:56,681
Line 18 is effectively doing this, so what
I'm going to do is comment out line 18,

27
00:02:57,120 --> 00:03:05,680
and put a new line of code here; teddy_list.append
parentheses, toy in double quotes.

28
00:03:05,680 --> 00:03:12,000
And obviously teddy_list was the one
that we defined on line three. So when we run this,

29
00:03:12,960 --> 00:03:18,000
you can see we've got the same output again.
So hopefully now, that's less surprising.

30
00:03:18,000 --> 00:03:24,164
We appended a new item to the teddy_list.
Both dictionaries contain a reference to that list -

31
00:03:24,164 --> 00:03:30,480
that's the value for teddy. Just to labour
the point, I'm going to replace line 11. Instead of

32
00:03:30,480 --> 00:03:34,787
calling a copy method, I'm going to do the copy
manually. This is what copy is actually doing. 

33
00:03:34,787 --> 00:03:40,880
So I'll copy that line out. Line 12, we're going to
type things is equal to. We'll put our left and

34
00:03:40,880 --> 00:03:50,018
right curly braces there. I'm going to copy each
line, so lion double quotes colon lion list,

35
00:03:51,040 --> 00:03:59,840
elephant, elephant double quotes colon
elephant list. And the third and final one,

36
00:04:00,720 --> 00:04:06,144
we've got teddy in double quotes, colon teddy_list.
So when we look at it that way,

37
00:04:06,144 --> 00:04:13,113
it's easier to see why animals square brackets
teddy in double quotes, on line 26, contained toy.

38
00:04:13,113 --> 00:04:18,000
We've got three different ways to refer to
the teddy_list, but they all refer

39
00:04:18,000 --> 00:04:22,460
to the same list, and you can see that more clearly,
looking at the dictionary references for animals

40
00:04:22,460 --> 00:04:28,010
and things, as we've defined it on screen. Alright,
so what I'm going to do is come down here, add a

41
00:04:28,010 --> 00:04:33,200
line after line 24. I'm going to add some code to
update the list, using each of the three references.

42
00:04:33,200 --> 00:04:37,968
We're already using the one on line 24, as you can
see, so I'm going to add the other two after that.

43
00:04:37,968 --> 00:04:45,440
I'm going to type animals square brackets,
teddy in double quotes, closing square brackets

44
00:04:45,440 --> 00:04:55,694
dot append parentheses and double quotes, added via
animals and the back quote. 

45
00:04:57,269 --> 00:05:03,518
Then we'll do the same thing on line 25 but for things,  
so it's things square brackets double quotes, 

46
00:05:03,518 --> 00:05:14,427
and teddy in those double quotes, dot append parentheses double quotes, added via. This time we'll put things

47
00:05:15,920 --> 00:05:21,200
And coming out down here, on line 29, we'll
print the teddy_list as well.

48
00:05:21,200 --> 00:05:24,987
Let's run the program and check the output,

49
00:05:26,640 --> 00:05:29,680
and there's the code. So whichever reference we use,

50
00:05:29,680 --> 00:05:34,320
the new items are added to the same list.
And when we print the list out, using each of

51
00:05:34,320 --> 00:05:40,743
the three references on lines 27 to 29, we get
exactly the same list. In the next video, 

52
00:05:40,743 --> 00:05:46,400
we'll see some slides explaining what goes on with
a shallow copy. So I'll see you in the next video.

