Create a Deep Copy of a List
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.
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
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.
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.
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.
Become a Member to join the conversation.