Skip to content
Advertisement

List behaviour in Python

I understand that the following code:

list_1 = [1]
list_2 = list_1
list_1[0] = 2
print(list_2)

gives 2 as output.

However, what I do not understand is that

list_1 = [1]
list_2 = list_1
list_1 = 2
print(list_2)

Does not give 2 as an output, but 1 instead?

Advertisement

Answer

A Python variable is a symbolic name that is a reference or pointer to an object. Multiple variables can point to the same object. Changing a variable assignment for one variable does not change the value of another variable.

Must understand what the code list_2 = list_1 vs list_1[0] = 2 means. The first is a variable assignment (or binding) and the second is object manipulation.
This is an important distinction.

  1. variable assignment
    The code list_2 = list_1 does NOT mean that list_2 is an alias to list_1 variable. The variables list_1 and list_2 are object references or pointers to objects. Changing list_2 or list_1 to reference to a different object will not affect the other.

  2. object manipulation
    The code list_1[0] = 2 changes the contents of the list object and not the assignment of the list_1 variable.

The following annotates the code statements with that each line is doing:

list_1 = [1]     => list_1 points to list object with value [1]
list_2 = list_1  => list_2 gets a reference to same object pointed to by list_1
list_1 = 2       => list_1 now points to object with value 2
print(list_2)    => list_2 still points to list object so value is still [1]

You can print out the identity of the object with the id() function which uniquely defines the object and is constant for the life of the object.

Here is a table showing the line of code in each step, values of each variable after code is executed, and id() of the variables to print the referenced object.

step code values id()
1 list_1 = [1] list_1 == [1] id(list1) = 2989298572224
2 list_2 = list_1 list_1 == [1]
list_2 == [1]
id(list1) = 2989298572224
id(list2) = 2989298572224
Both point to same object
3 list_1 = 2 list_1 == 2
list_2 == [1]
id(list1) = 140706891962064 => different object
id(list2) = 2989298572224

After step 2, both variable list_1 and list_2 point to a list object with value [1].
After step 3, list_1 points a different object (i.e. integer object), but list_2 is unchanged so printing list_2 at this point prints [1] not 2.

A related tutorial may help to visualize variable assignment and object references.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement