0

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.

Floris
  • 25
  • 5
  • Sorry your question is unclear: An array (a list actually) is a container. It can contain anything. What do you mean with compare an array to the function of an URL? – Jean-Marc Volle Jun 07 '20 at 13:23
  • This is a very weird description. Python does not have arrays (outside of an obscure module and third party extensions). Names are never copied, they are not first class. The memory layout is an implementation detail. And so on... – MisterMiyagi Jun 07 '20 at 13:25
  • Does this answer your question? [Are python variables pointers? or else what are they?](https://stackoverflow.com/questions/13530998/are-python-variables-pointers-or-else-what-are-they) – MisterMiyagi Jun 07 '20 at 13:26
  • Thank you for your replies. I thought an array & a list were two different things. When I read the text of the Python course, it looks like list2 is empty in itself. List2 only refers to list1. That's why I thought an array is some kind of link or address... – Floris Jun 07 '20 at 13:37
  • Can you please [edit] your question to clarify what you are actually asking? Are you wondering how lists work? Are you wondering how names work? Are you wondering how modifying mutable objects works? – MisterMiyagi Jun 07 '20 at 13:50
  • Of course I can adjust my question, MisterMiyagi. I wanted to know what an array exactly is. The text in the Python course about the array wasn't clear to me. How can I change it best? Suggestion: "Python: what's exactly an array?" – Floris Jun 07 '20 at 13:55
  • I think the text you read is about references. It has nothing to do with what an array is. Please clearify your question. – elementzero23 Jun 07 '20 at 14:03
  • Thank you for your comment, elementzero23. How can I adjust it best? – Floris Jun 07 '20 at 14:06
  • Please specify what *object* you inquire about - a ``list``, ``array.array``, or ``numpy.array``? Clarify what about them you *do not understand* - how they store items, how mutation is seen across aliases, ...? What level of detail are you looking at? Is your question implementation dependant? Also, details as closely as possible what parts you *do already understand*. – MisterMiyagi Jun 07 '20 at 14:07
  • Thank you all for the comments. I made a title change & I linked the array tag to the question. Is that ok? I am new here and I try to adjust the question to Stack Overflow's standard. – Floris Jun 07 '20 at 14:17
  • Please also [edit] the body of the question, not just the title. The title is very broad, and the text is misleading, making it impossible to tell what exactly you are asking for. See the [ask] page for details on how to ask an on-topic question. – MisterMiyagi Jun 07 '20 at 14:23
  • Thanks again for your comments, MisterMiyagi. I have adjusted the question. What do you think of the alterations? Is it an improvement? If not, what can I change best? Thank you for your time and effort. – Floris Jun 07 '20 at 19:28
  • I'm afraid not. The last edit has only changed formatting and removed information. Please see my previous comments on the minimum information the question should attempt to provide. – MisterMiyagi Jun 08 '20 at 11:34
  • For me, Python is completely new for me. So I am first trying to understand the basics. This is not really an in depth question, but a more general one. Ok, a new attempt - new title suggestion: Python: How can I grasp the general concept of an "array" in Python best? Body of the question: 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." – Floris Jun 08 '20 at 18:52
  • The title is not the problem. The body of the question is way too vague. You have not even defined what you mean by array – the term can mean multiple things in Python. Citing some course is not helpful either, when it is not clear which course. – MisterMiyagi Jun 08 '20 at 18:55
  • Thank you for the update. I did not know that an array can mean multiple things – Floris Jun 08 '20 at 18:58
  • Now my updated question: – Floris Jun 08 '20 at 18:58
  • In the lesson 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? – Floris Jun 08 '20 at 19:07
  • What do you think about the new explanation of my question, MisterMiyagi? – Floris Jun 08 '20 at 19:08
  • I added the source as well. That is a good comment. I forgot to mention the source. – Floris Jun 08 '20 at 19:12
  • 1
    Your question needs to be self-contained. Telling us what chapter of a book the code is in does us no good if we do not have the book. You need to include the code [in your question](https://idownvotedbecau.se/nocode/). – Dour High Arch Jun 08 '20 at 21:17
  • @Dour High Arch : I looked at your remark. I thought that I could add the complete chapter (at the end) best so everyone has a complete picture of the chapter itself. – Floris Jun 10 '20 at 09:50
  • Sorry, but that excerpt still does not make it clear what is meant by "array". It might be a misnomer of "list", but it is not clear. Note however that the statements about variables are plain wrong. Variables never store content, and do not separate scalar/complex. See also [If two variables point to the same object, why doesn't reassigning one variable affect the other?](https://stackoverflow.com/questions/56667280/if-two-variables-point-to-the-same-object-why-doesnt-reassigning-one-variable) and [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html). – MisterMiyagi Jun 10 '20 at 10:55
  • Thanks again for your comments, MisterMiyagi. To be honest I am glad to hear that I am not the only one who was puzzled by the text of the course "PCAP - Programming Essentials in Python". Experts like you can pinpoint exactly what is not up to par. Thank you for the useful links. Much appreciated. – Floris Jun 10 '20 at 15:30

3 Answers3

2

Your first statement means that the new list list2 is a reference to list1
EDIT : list2 is actually not a new list, it is a reference to list1
Check This :

list1 = [1,2,3]
list2 = list1
print("List1 = ",list1,"List2 = ",list2)

Gives :

List1 = [1,2,3] List2 = [1,2,3]

And

list1 = [1,2,3]
list2 = list1
list2[0] = 4
print("List1 = ",list1,"List2 = ",list2)

Gives :

List1 = [4,2,3] List2 = [4,2,3]

In this case, any changes made to the second list is reflected in the first list as you are actually changing the same object using different names
You should use the copy module to create a new object :

import copy
list1 = [1,2,3]
list2 = copy.deepcopy(list1)
print("List1 = ",list1,"List2 = ",list2)
list2[0] = 4
print("List1 = ",list1,"List2 = ",list2)

Gives :

List1 = [1,2,3] List2 = [1,2,3]
List1 = [1,2,3] List2 = [4,2,3]
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
1

The reason this happens is something called a pointer.

A pointer is simply a variable to a location in memory. This is how python lists work. When you set

foo = [1,2,3,4,5]

python sets the value of foo not to the actual list, but instead, to a memory location in the computer where the actual list is stored. Therefore, foo is a pointer - a variable with a memory location.

So, when you then set

bar = foo

This sets bar to the memory location stored by foo. Foo and bar are now both set to the same memory location.

As a result, if you then say:

bar.append(6)

The value stored at that memory location is changed. So now if you say

print(foo)

This prints [1,2,3,4,5,6].

This is a simplified explanation. Hope I helped!

Edit

Also, there is a difference between a typical array and a python list. The most important difference is that the length of an array cannot be changed, i.e. there is no append. You can only change the value of the array's elements.

In python lists, you are free to resize the list as you wish.

tersrth
  • 861
  • 6
  • 18
1

Great question. You're looking for the term aliasing. Also take a look at the id function, which will help you internalize this idea. For our purposes, id() returns the unique identity of a variable.

If your course was right, we'd expect both lists to have the same id. Like so:

>>> numbers1 = [1, 2, 3]
>>> id(numbers1)
140675313832392
>>> numbers2 = numbers1
>>> id(numbers2)
140675313832392

Your course was indeed right.

Can I compare an array with the function of an URL? If not, what's the best way to describe an array?

I think you're narrowing the concept only to arrays, which would be a mistake. The same holds for all variables in Python. Proof? Try the id experiment with integers, for example.

Hope it helps, good luck!