Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Create a Deep Copy of a List

00:00 That’s probably not what you want to achieve. So if you really want to create a deep copy of a list object—this is what a copy is called that also takes care of copying the contained objects—then you can do this by importing a module called copy from the standard library.

00:20 Let’s look at that. Next, you can say import copy. And then now I’ll have to reset the matrix list to its original value. First, matrix looks like this.

00:34 Let’s get rid of the_matrix. I just deleted it now. So we currently don’t have the_matrix variable name, but matrix points again to [1, 2] and [3, 4].

00:47 And now I want to create a deep copy of the matrix object so that then I can apply these changes. And I can do that by saying the_matrix = copy. So this uses the copy module that I’ve imported and then copy.deepcopy().

01:04 So I’m using the deepcopy() function from the copy module, and then I’m passing it the element that I want to copy, so here, the matrix list.

01:13 And now you can look at this again. The matrix object is not equal anymore to the ID of the the_matrix object.

01:25 These are not, don’t have the same value, so they don’t point to the same location in memory, which means that they’re separate objects, which the same holds true now also with the contained references.

01:38 So matrix[0], for example, is not equal anymore to the ID of the_matrix,

01:49 the first list contained in the matrix. What did I do?

01:54 Did a double x in here, right? Let me do that again

01:59 and get rid of this double x. And here you can see that again, they point to different locations in memory, which means that now you can apply both of these changes that you wanted to do, and you could go arbitrarily deep.

02:11 Now if you use a deep copy, then any element that’s inside of the list, any reference that’s in there, is going to create a separate object again. So now I can do, oops,

02:25 I want to change the second list to point to the movie character names, the_matrix = ["Neo", "Trinity"] and matrix points still to the integers.

02:35 And now let’s also try the second thing that threw us for a loop earlier. Okay, here it is, the_matrix[0][0]. So the first list inside of the_matrix, which currently points to the two integers.

02:50 And the first element, which here is a 1, I want to change to "Morpheus".

02:55 the_matrix changed, and matrix is still the same. And that’s because you’ve created a deep copy of the initial matrix list.

03:09 Only with a deep copy can you create a truly independent copy of an object. So that’s something to keep in mind and a tricky bit that comes with the mutability of list elements.

03:21 Make sure that you try out the code that I’ve shown you here because this is a slightly tricky piece of information to digest. Keep in mind that variables are references to objects and that lists contain those references.

03:34 Like, if you keep these two pieces of information in mind, it’ll make sense that you’re just changing the value of an object somewhere in memory. And then there’s separate ways that this object can be accessed.

03:45 And if you really want to make a copy, you’re going to have to copy all of the references in there, basically, which you can do with this copy.deepcopy() function.

03:54 Okay, tricky bit. Train it, think through it again. And I hope it’ll make sense with this explanation that I just gave you here. And we’re nearing the end of this course already.

04:05 So in the next lesson, you’re going to take a look at how you can sort lists.

Become a Member to join the conversation.