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 Second Reference to a List Object

00:00 In this lesson, you’ll learn about copying lists and some of the tricky bits that come with the fact that lists are mutable. And I’ll start this one off in IDLE.

00:10 Let’s begin by creating a list that I’ll call matrix.

00:15 It’s a list that contains two lists, and the first of the contained lists will contain the integers 1 and 2, and the second one, the integers 3 and 4.

00:27 Okay, so this is our matrix list. Now imagine that you want to make a copy of this list. What do you think you might be able to do is just set it equal to that list.

00:38 So let’s say you want to create another list that you’ll call the_matrix,

00:43 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.

00:57 However, variable names in Python are just references to objects in memory, which means that both of these variable names now, matrix and the_matrix, they point to the same object in memory.

01:10 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 3 and 4, I’m going to change this second contained list

01:31 that would be at index one.

01:34 And you want to change it to a list that contains the names of the characters of the Matrix movie. So the main characters are called Neo and Trinity.

01:47 So you want to make that change. And now if you look at the_matrix, it seems like the list now contains one list at the first index, which still contains the integers 1 and 2.

01:59 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.

02:20 So if you apply a change through one of the variable names, the object’s going to change, and that’ll be represented also if you look it up through the other variable name.

02:30 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.

02:44 Say id(), and then pass it an object—for example, matrix. In this case, you get a big number that represents the location in memory of that object.

02:54 And now if you look at the id() of 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.

03:05 They point to the same object.

03:09 So the takeaway here is that variable names in Python are references to objects in memory. You cannot create a copy of a list by just saying one variable name equals another variable name.

Become a Member to join the conversation.