r/pythonhelp • • Dec 26 '22

Setting one array equal to another causes them to mirror each other, why?

I simply don't understand what's going on here. I have the following code that produces the following result:

list = [1, 2, 3]
print("list:", list)
newList = list
print("newList:", newList)
newList.append(1000)
print("Appended newList:", newList)
print("list:", list)

Result:

list: [1, 2, 3]
newList: [1, 2, 3]
Appended newList: [1, 2, 3, 1000]
list: [1, 2, 3, 1000]

Why in the world does list get appended when I append newList? The same doesn't happen for variables so I can't seem to understand why it would happen for arrays.

2 Upvotes

10 comments sorted by

3

u/carcigenicate Dec 26 '22

It has the same effect for every type of object. a = b means "make a refer to the object b is referring to. It makes both names refer to the same exact object.

1

u/pat-work Dec 26 '22 edited Dec 26 '22

If you do that with variables it doesn't refer to the same object, that's why I'm confused.

If you do:

x = 10
y = x
x += 10

y is still 10 and x is now 20.

2

u/Goobyalus Dec 26 '22
x += 10 

means

x = x + 10

which means that you're assigning x to refer to a different object that came from x + 10

Appending to a list is not an assignment, it mutates the underlying object.

2

u/pat-work Dec 26 '22

Thank you for the answer! That makes it a lot more clear :)

1

u/carcigenicate Dec 26 '22 edited Dec 26 '22

First, I'm not sure what you mean exactly by "variables" since all examples on this page use variables. Your original code uses lists and this code uses integers.

Second, += is very different from append. For integers and other immutable objects, += is equivalent to, for example, x = x + 10. This doesn't alter the object referred to by x. All = does is change what object x is referring to. x += 10 takes the value of x, and then reassigns x to refer to the integer object 20. This doesn't change the 10 object though (since 10 is immutable, so it can't change), so the y variable still refers to 10.

append on the other hand actually alters the object it's called on (the object the variable newList is referencing). Because it alters/changes/mutates the object itself, any other variables looking at that object will see the change.

1

u/pat-work Dec 26 '22

Thank you for the in depth explanation! That makes a lot more sense. So in theory I could create an object that behaves like 10 does, but instead make it mutable? Let's say: x = myClass.MyNumbers(10) and then set y = x and then use something like y.add(10) which would make both x and y equal 20. Is that correct? I know this is probably never ever useful, but I'm just theorizing here so I can understand how everything works.

1

u/carcigenicate Dec 26 '22

Yes, that's how that would work if add altered some internal variable. I recommend actually trying that because it would not be very hard to whip up a small example like that, and it would be a good exercise.

1

u/pat-work Dec 26 '22

I'll give it a try, again thank you very much!

1

u/carcigenicate Dec 26 '22

Also, look into the id function if you haven't already. You can use that to see if two names refer to the same object, which is handy when learning and debugging.

1

u/AmongstYou666 Dec 27 '22

a = 10

b = a.copy()