Create a Second Reference to a List Object
and then maybe you would want to make the copy by just saying equals
matrix, right? So did that work? If you look at
the_matrix, it looks like you have another list that has the same contents as the original list.
And with mutable objects, this can have surprising effects. Now, if you go ahead and say you want to change something in
the_matrix, you go ahead and instead of having a list that points to the integers
4, I’m going to change this second contained list
And then the second element in your list now points to another list that contains the two names. However, because the variable name
the_matrix is just a reference to an object in memory, if you try to access that object using the
matrix variable name, you’ll see that you get the same result because both of these point to the same object.
So if you use the syntax of saying one list equals another list, you’re not creating a copy. You are just creating a second reference to that same object. And you can look at that by using the
id() function in Python.
And now if you look at the
the_matrix, you’ll see that this is the same number. So you can compare these two. And both of these numbers are the same, and that’s because they are the same object in memory.
Become a Member to join the conversation.