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

Create a Shallow Copy of a List

00:00 But how can you create a copy? Let’s reset the matrix list to the initial values

00:07 and then I’ll show you how you can create a shallow copy in Python. You can use a similar syntax to the one that didn’t work for what we’re intending to do, but instead of saying just the_matrix = matrix, at the end of the original list, you’re going to use slice notation.

00:25 So open square brackets, close them and put a colon in between. Now, if you remember how slicing works, this shortcut says take everything that’s inside of the list, like from the beginning to the end.

00:38 By leaving out the two index numbers on the left and the right side of the colon, you just take the whole list.

00:44 So this syntax creates a shallow copy of the list, which means that the ID of the_matrix is now different to the ID of matrix.

00:57 You can see that they point to different numbers because now the_matrix and matrix are two different objects in memory, two separate lists.

01:07 So now you can do what you tried to do before, and it’ll work out. So if I now go ahead and say the_matrix[1] and change the element on index one to the names of the two movie characters, and then I can look at the_matrix, and it does what you expect it to do.

01:24 But matrix didn’t change in that case because like I said, the_matrix and matrix are now two separate objects in memory.

01:32 So this is how you can create a shallow copy in Python. You can use the [:] slice notation, and that’ll create a separate list object in memory.

01:47 Okay, but the trickiness with copying lists doesn’t end here because a shallow copy doesn’t actually copy everything that’s inside the list. It just copies the references inside.

01:59 You can see that the two lists that you’re working with here, the_matrix and matrix, they contain other lists. And these list objects don’t actually contain the objects.

02:07 They just contain references to those objects, which means that the same issue that you’ve dealt with earlier still applies on a shallow copy if you step one level lower.

02:19 Let’s try that out. I’ll start again with resetting. No, I don’t need to reset anything because matrix still looks the same. Okay, if you now went ahead and you wanted to change inside of the_matrix, you want to change also something in the first list that’s contained by the_matrix.

02:38 So you access the first element in the_matrix, which is this list of two integers, 1 and 2, and now you want to change the first element of that list.

02:47 So I’m going to say the_matrix[0][0], and that should point to the integer 1 in here. And now I want to change this one to also be the name of a character.

02:58 So let’s change that to "Morpheus".

03:03 Okay. the_matrix changed as you expect it to. Now it contains still two lists, but the first list now also has one name of a movie character, and one is still the integer from before.

03:14 However, if you now look at matrix, then you’ll see that "Morpheus" also appears in here. And why is that? Because like I said before, the two separate list objects, the_matrix and matrix only contain references to the other list objects in there.

03:31 So they don’t actually contain a list [1, 2] but they contain a reference that points to another list object that sits somewhere in memory and contains the values 1 and 2.

03:42 And if you create a shallow copy like you did before using the slice notation, you don’t create separate objects from the containing lists. So if you look at id(the_matrix[0]), so the first element in the_matrix, which is this list, or well now it looks like that it contains now "Morpheus" and 2.

04:06 So that’s the big number of its memory location. And if you do the same thing on matrix,

04:12 get the first element in matrix again, you can see that these big numbers point to the same object.

04:23 So the takeaway here is that shallow copies contain copied references. They don’t copy the objects inside of it, but they just contain a copied reference, which means that if you do a change to a mutable object in there, again, you’ll have the same issue that you’re changing the object.

04:39 And that will be reflected in any other object that has a reference to that changed object.

Become a Member to join the conversation.