In my Python course I read these sentences:
"The assignment: list2 = list1
copies the name of the array, not its contents. In effect, the two names (list1 and list2
) identify the same location in the computer memory. Modifying one of them affects the other, and vice versa."
Source available on Cisco Networking Academy - Course name: PCAP - Programming Essentials in Python - module 3, chapter 3.1.6.1 titled "The inner life of lists". Completely below you encounter the complete text of chapter 3.1.6.1.
In chapter 3.1.6.1 the author explains what a list is and its functionalities. The paragraph I mentioned above raises the question of how I can best can visualize an "array". Can I see an "array" as a link to a list only? Is an "array" in itself an empty entity?
Complete text of chapter 3.1.6.1:
The inner life of lists
Now we want to show you one important, and very surprising, feature of lists, which strongly distinguishes them from ordinary variables.
We want you to memorize it - it may affect your future programs, and cause severe problems if forgotten or overlooked.
Take a look at the snippet in the editor.
list1 = [1]
list2 = list1
list1[0] = 2
print(list2)
The program:
– creates a one-element list named list1
;
– assigns it to a new list named list2
;
– changes the only element of list1
;
– prints out list2
.
The surprising part is the fact that the program will output: [2], not [1], which seems to be the obvious solution.
Lists (and many other complex Python entities) are stored in different ways than ordinary (scalar) variables.
You could say that:
– the name of an ordinary variable is the name of its content; – the name of a list is the name of a memory location where the list is stored. Read these two lines once more - the difference is essential for understanding what we are going to talk about next.
The assignment: list2 = list1
copies the name of the array, not its contents. In effect, the two names (list1 and list2
) identify the same location in the computer memory. Modifying one of them affects the other, and vice versa.