a=[1] # here is a comment to the right
print("as expected, a=", a)
b=a
print("as expected, b=", b)
a[0]=2
print("as expected, now a=", a)
print("NOT EXPECTED TO CHANGE now b=", b)
Asked
Active
Viewed 32 times
-2

Joran Beasley
- 110,522
- 12
- 160
- 179

Bella
- 11
- 1
-
its terrible that stackoverflow does not allow me to enter comments next to each line.... – Bella Feb 15 '20 at 18:28
-
@Bella you certainly can put comments next to each line ... not sure what you mean – Joran Beasley Feb 15 '20 at 18:30
-
I changed the value of the a list, from 1 to 2 and it did indeed change. BUT I did not change the value of the b list, and yet, it appears to have changed as well to 2 also... How could that be? – Bella Feb 15 '20 at 18:30
-
LeoE, can you please explain more? I dont really understand.... – Bella Feb 15 '20 at 18:30
-
first I entered the code as this line here a=[1] # a is now [1] ie I entered comments to the right of each, but the system kept telling me it cannot post it as it was, it was not properly formatted.... the code was right, we can have comments to the right... – Bella Feb 15 '20 at 18:31
-
LeoE, that was my problem, the system would not allow me to have the comment to the right of the code, it kept telling me not well formatted... – Bella Feb 15 '20 at 18:33
-
edited your question to include a comment. .. – Joran Beasley Feb 15 '20 at 18:33
-
Be sure to read https://nedbatchelder.com/text/names.html – chepner Feb 15 '20 at 19:37
2 Answers
2
lists are mutable ... both a and b point to the same list, so changing a will also change b
if you do not want this behaviour assign a copy of a to b
b = a[:]
if you had used an immutable datatype (like a string or number) when you change it it assigns a completely new variable to b
and works ... but mutable datatypes wont work like this

Joran Beasley
- 110,522
- 12
- 160
- 179
-
Can you elaborate more on the "both a and b point to the same list"? How is that? Also, the fact that lists are mutable might not be relevant here, right? – Bella Feb 15 '20 at 18:35
-
are you saying that when we entered b=a that made b pointing to the same memory as a? Doesnt b have its own memory? – Bella Feb 15 '20 at 18:36
-
2@Bella as for `"both a and b point to the same list"` - run your code on http://pythontutor.com/ and you will see this as arrows from `a` and `b` to the same element. – furas Feb 15 '20 at 18:39
-
yes they both point to the same list in memory. .. all B and A are is pointers (ie its an address to the memory location of the actually list (check with `a is b`) – Joran Beasley Feb 15 '20 at 18:39
-
@furas I did run it in pythontutor.com and saw the arrows, very enlightening... Many Thanks, I appreciate that. – Bella Feb 15 '20 at 18:42
-
@Joran Beasley I tried copying like you suggested c = a[;] but when a was changed, c was changed too... – Bella Feb 15 '20 at 18:45
-
@furas do you happen to know any other online python site to run code, like to import files, too and also do file io from files on my desktop? – Bella Feb 15 '20 at 18:48
-
@Bella all online pages can work only with files uploaded on server - eventually with files on other pages like online drivers - ie. [Google CoLab](https://colab.research.google.com/notebooks/intro.ipynb) can work with files on Google Drive. They can't access directly files on desktop for security reason. – furas Feb 15 '20 at 19:19
1
In python (as in many languages, with the notable exception of C/C++), names are references to values. When you write a = b
, you make a
refer to the same value as b
, you don't copy its contents.
Use b = list(a)
to create a copy (or see deepcopy
for more complex objects)

blue_note
- 27,712
- 9
- 72
- 90
-
are you saying that b=a makes be point to the same location as a for ever from now on? How can I assign the contents of a to be only, and NOT make it point to same location? – Bella Feb 15 '20 at 18:38