r/pythonhelp • u/pat-work • 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
1
3
u/carcigenicate Dec 26 '22
It has the same effect for every type of object.
a = bmeans "makearefer to the objectbis referring to. It makes both names refer to the same exact object.