If y = x is followed by a change to y and x appears to change too, the reason is usually that both names refer to the same mutable object. Assignment creates another reference; it does not copy the object.
What happens when you write y = x?
Python makes y refer to the object that x currently refers to. There is one object and two names for it. The Python Software Foundation explains this directly in its Programming FAQ.
As an Amazon Associate I earn from qualifying purchases.
x = []
y = x
y.append(10)
print(x) # [10]
print(y) # [10]
append changes the list in place. Since both names refer to that list, looking through either name reveals the added value.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesWhy does changing an integer behave differently?
Integers are immutable: an operation does not change the existing integer object. In this example, addition produces a value and the assignment makes x refer to it; y still refers to the original value.
#1 Best Overall
x = 5
y = x
x = x + 1
print(x) # 6
print(y) # 5
This is rebinding, not a shared mutation. Rebinding changes the object a name refers to. Mutation changes an object itself, which is why it can be observed through every name referring to that object.
Which Python objects can be changed in place?
Lists, dictionaries, and sets are mutable: operations can change their contents without replacing the object. Numbers, strings, and tuples are immutable with respect to their own value or structure. The Python data model describes these distinctions.
Rank #2
Immutability applies to an object’s own structure, not necessarily to everything it contains. A tuple cannot have its item slots reassigned, but it can contain a mutable object such as a list:
items = [1]
t = (items,)
t[0].append(2)
print(t) # ([1, 2],)
The tuple itself was not changed; the list it refers to was.
How can you tell whether an operation mutates or creates a value?
Look at the operation and the type. In-place list methods such as append and sort change the existing list. By contrast, y = y + [10] makes a new list and rebinds y; sorted(y) returns a new sorted list and leaves the original unchanged. Many mutating methods return None, rather than returning the modified object.
Augmented assignment is type-dependent. For a list, += can update the existing list in place; for an integer, it produces a new value and rebinds the name. Do not assume that the same operator has identical mutation behavior for every type.
How do you make a copy instead of another reference?
Choose the copy depth based on whether nested objects should remain shared. A shallow copy creates a distinct outer object but retains references to objects inside it. A deep copy recursively copies nested objects, subject to the objects’ copying behavior.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →import copy
x = [[1]]
y = copy.copy(x) # new outer list; inner list is shared
z = copy.deepcopy(x) # nested list is copied too
For a shallow copy, changing the outer list does not change the other outer list, but mutating the shared inner list is visible through both. Use copy.deepcopy() when nested objects should not remain shared and recursive copying is appropriate.
Best Value
To check whether two names refer to the identical object, use x is y. The built-in id() can also provide an object’s identity value during a program’s lifetime, but equality with == answers a different question: whether values compare equal.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




