Python variable question - how does this work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python variable question - how does this work

a = [1,2,3] b = a b.append(5) a.append(4) print(a) print(b) Result [1,2,3,5,4] [1,2,3,5,4] Why isn't a [1,2,3,4] and b [1,2,3,5]? Edit: And why is this different a = "hello" b = a a += "world" b += "!" print(a) print(b) Result Helloworld Hello!

6th Aug 2021, 6:33 PM
Yvonne
Yvonne - avatar
11 Answers
+ 5
The reason is that b = a creates a shallow copy of a. That means that a and b refer to the same object, not to two identical objects. For more information about shallow and deep copies: https://realpython.com/copying-JUMP_LINK__&&__python__&&__JUMP_LINK-objects/
6th Aug 2021, 6:40 PM
Simon Sauter
Simon Sauter - avatar
+ 7
because b is referring to a when you give b=a.So if you change the value of b or a it will affect both. If you want to create duplicate instead of reference, you can use list slicing .Like that: b=a[:] https://code.sololearn.com/c4i4vuKlD5J2/?ref=app
6th Aug 2021, 6:38 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 4
When you assign things in python they then reference to the same location . So any change to values stored in variable a or b will affect both .
6th Aug 2021, 6:39 PM
Abhay
Abhay - avatar
+ 2
Yvonne Rizkallah In Python, everything is an object and every initialized variable holds the reference to an object present in the heap. Here's what happens in your code: # Create an array object and store its reference in variable 'a'. a = [1, 2, 3] # Assign this reference contained in variable 'a' to variable 'b'. b = a # Call the append method of the list object b.append(5) a.append(4) # Since both 'a' and 'b' are the same (as mentioned earlier), printing 'a' and 'b' shows the same result. Now, considering the edit: # This creates a new instance (a new string object) of the str class and assigns its reference (i.e., the memory location that contains the data related to the object) to variable 'a'. a = "hello" # This copies the reference contained in variable 'a' to variable 'b' b = a # This creates 2 different str objects, the first one being the 'world' string and the second one being a + 'world', the concatenated version (note: a+="world" is the same as a = a + "world") a += "world" Continued...
6th Aug 2021, 7:20 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
# Do the same thing to variable 'b' b += "!" Clearly, both the variables contain different references to two different string objects, hence the differing outputs. I hope that this explanation helps.
6th Aug 2021, 7:22 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Yvonne Rizkallah The assignment is the same. However, what happens next is not. In the first case, a method is called. Both the variables 'a' and 'b' refer to the same object. So, the method can be called in either one. But the second case is a bit different. When concatenating strings, a new string object is created and the reference to this is returned. Since strings are immutable, the original string can't be modified. The only way is to make a new string object. 'a + "world"' and 'b + "!"' create two new string objects, even though 'a' and 'b' were initially the same. Please read my previous comment to understand about this better.
6th Aug 2021, 7:27 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
The reason it didn't work for the list because a and b are pointing to the same object as what Simon Sauter said but, you can instead do: a = [1, 2, 3] b = a[:] print(a) print(b) result: [1, 2, 3, 4] [1, 2, 3, 5] Which will create a copy of the list stored in a and store it in b https://code.sololearn.com/c6FI3vR1PcxV/?ref=app
8th Aug 2021, 4:04 PM
Rayshawn
Rayshawn - avatar
+ 2
Or you can just use the list.copy() method to make a distinct, but duplicated copy of the original list. Note: Only the list object is copied, not the objects inside them.
8th Aug 2021, 4:10 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
8th Aug 2021, 4:11 PM
Rayshawn
Rayshawn - avatar
+ 1
Calvin Thomas why is the list assignment different than a string assignment
6th Aug 2021, 7:23 PM
Yvonne
Yvonne - avatar
+ 1
The reason is that you created a shallow copy as b=a when you assign anything then they refer the same location
7th Aug 2021, 8:22 AM
K.Lohitha Siri
K.Lohitha Siri - avatar