1
00:00:05,280 --> 00:00:10,880
In this video, we'll go through the shallow
copy process, step by step. I'll use slides to

2
00:00:10,880 --> 00:00:17,487
show what's happening, when you perform a shallow
copy of a container that contains mutable objects.

3
00:00:18,560 --> 00:00:21,827
We start off with a dictionary
whose values are lists.

4
00:00:21,827 --> 00:00:24,720
This is the same dictionary we
had in the previous video.

5
00:00:24,720 --> 00:00:29,917
Note that the values that are stored inside
the dictionary aren't lists. They're references

6
00:00:29,917 --> 00:00:38,141
to lists. The value for lion is a reference to a
list, and the list exists somewhere else in memory.

7
00:00:38,480 --> 00:00:42,240
We use the copy method to
create a copy of the dictionary.

8
00:00:42,240 --> 00:00:47,822
Python starts by creating a new
dictionary. We call the copy, things.

9
00:00:49,280 --> 00:00:53,440
Python then goes through each of the
dictionary keys. It copies each key,

10
00:00:53,440 --> 00:00:58,960
and its value, into the new dictionary.
Here we can see the key lion has been

11
00:00:58,960 --> 00:01:05,200
copied into the things dictionary. The value
is also copied. This means the value for lion

12
00:01:05,200 --> 00:01:12,320
is a reference to the same list that's
being referred to in the animals dictionary.

13
00:01:12,320 --> 00:01:17,857
The value of the lion key in animals, and the
value of the lion key in things, both refer

14
00:01:17,857 --> 00:01:24,800
to the same list. We're performing a shallow
copy here. The list isn't copied. A shallow copy

15
00:01:24,800 --> 00:01:32,247
copies references. It doesn't make a copy of
the things that are being referred to. 

16
00:01:32,247 --> 00:01:41,338
The next key is copied in exactly the same way. Here, Python
copies the elephant key into the things dictionary.

17
00:01:42,400 --> 00:01:47,520
Once again, it's the reference to the list
that gets copied, not the list itself. 

18
00:01:47,520 --> 00:01:54,320
There's only one list for elephant, and the values
in both dictionaries are referring to it.

19
00:01:54,320 --> 00:01:58,207
Our original dictionary only had
three keys - which is a good thing,

20
00:01:58,207 --> 00:02:04,994
or these slides would go on for a long time. Once
again the key is copied from animals into things.

21
00:02:06,480 --> 00:02:11,366
And once again, it's the reference to
the teddy_list that's copied.

22
00:02:12,880 --> 00:02:18,800
We end up with a copy of the original dictionary,
containing the same keys and values. That means the

23
00:02:18,800 --> 00:02:25,197
values will continue to refer to the same objects
in the copy. As you can see from these slides,

24
00:02:25,197 --> 00:02:29,840
there's only one lion_list,
and both dictionaries refer to it.

25
00:02:29,840 --> 00:02:33,928
The same for elephant underscore
list and teddy underscore list.

26
00:02:35,680 --> 00:02:40,880
If the values are immutable, such as strings or
ints, then we really don't care about references

27
00:02:40,880 --> 00:02:47,440
being copied. If we can't mutate a value, then none
of this really matters. Where it becomes important

28
00:02:47,440 --> 00:02:53,920
to understand all this, is when the values
refer to mutable objects. As we saw, adding toy

29
00:02:53,920 --> 00:03:00,800
to the teddy value in things, resulted in toy
appearing in the teddy value for animals too.

30
00:03:00,800 --> 00:03:06,092
Now that you know how a shallow copy works,
that shouldn't be surprising any longer.

31
00:03:07,440 --> 00:03:12,897
If you need to create a deep copy of an object,
Python has a copy module that you can use. 

32
00:03:12,897 --> 00:03:20,000
The copy module provides a deep copy function. Performing a
deep copy will create copies of lists, dictionaries

33
00:03:20,000 --> 00:03:24,960
and other mutable objects, that are contained in
whatever you're copying. If you want to experiment

34
00:03:24,960 --> 00:03:31,143
with a deep copy, check out the documentation. The link
is on screen and in the resources section for this video.

35
00:03:31,143 --> 00:03:35,173
Using the function is easy. The hard part
is understanding the difference between a

36
00:03:35,173 --> 00:03:40,747
deep and shallow copy, and you understand that
now. Alright, so moving on. In the next video,

37
00:03:40,747 --> 00:03:45,840
we'll perform a deep copy to demonstrate
the difference. See you in the next video.

