Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

If y = x and both variables refer to a mutable object such as a list, changing that object through y also changes what you see through x. The assignment creates a second reference to the same object; it does not copy the object.

What happens when you write y = x?

Python binds the name y to the object that x already refers to. There is still one object, now accessible through two names. As the Python Software Foundation’s Programming FAQ explains, assigning one list variable to another does not create a copy.

x = []
y = x
y.append(10)

print(x)  # [10]
print(y)  # [10]

append changes the list itself. Since both names refer to that list, looking it up through either name shows the change.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Mutation and reassignment are different

A mutation changes an existing mutable object. Reassignment instead changes which object a name refers to; it does not rewrite the other name’s reference.

Mutating a shared list

x = [1, 2]
y = x
y.append(3)

print(x)  # [1, 2, 3]
print(y)  # [1, 2, 3]

The list is shared, and append modifies it in place.

Rebinding a name

x = [1, 2]
y = x
x = [1, 2, 3]

print(x)  # [1, 2, 3]
print(y)  # [1, 2]

The final assignment makes x refer to a new list. It does not alter the list still referenced by y.

Immutable values

x = 5
y = x
x = x + 1

print(x)  # 6
print(y)  # 5

Integer addition produces a value rather than changing the integer 5. The assignment binds x to the result, while y remains associated with 5. Numbers and strings are immutable; lists, dictionaries, and sets are mutable. The Python data model documentation describes these distinctions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to tell whether an operation changes an object

Some list operations mutate the existing list; others produce a result that can be assigned to a name. The distinction matters when another name refers to the original object.

Operation Effect What to expect from another reference
y.append(10) Mutates the existing list Sees the changed list
y.sort() Mutates the existing list Sees the reordered list
y = y + [10] Creates a new list and rebinds y Still refers to the original list
sorted(y) Returns a new sorted list Still refers to the original list

Many mutating methods in Python’s standard library return None rather than the modified object. For example, y.sort() changes y in place, so use it as a statement rather than expecting a sorted list as its return value.

Augmented assignment needs attention: += can mutate a list, whereas with integers it produces a new value. Its effect depends on the type involved.

How to make a copy instead

Use a copy when you want a separate outer object. Choose shallow or deep copying based on whether nested objects should remain shared. Python’s FAQ discusses copy.copy() and copy.deepcopy().

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Shallow copy

A shallow copy creates a new outer object but keeps references to the original’s contents. With a flat list of numbers, that is often sufficient:

import copy

x = [1, 2]
y = copy.copy(x)
y.append(3)

print(x)  # [1, 2]
print(y)  # [1, 2, 3]

Deep copy

A deep copy recursively copies nested objects too. Consider it when changes to nested mutable values should not be shared. Deep copying may not be appropriate for every object, so use it when that independent nested structure is what your code requires.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A tuple can still contain mutable data

Immutable describes the container’s own structure, not every object reachable from it. A tuple cannot have its elements replaced, but if one of those elements is a list, that list can still be mutated:

items = ([1, 2],)
items[0].append(3)

print(items)  # ([1, 2, 3],)

The tuple still contains the same list; the list’s contents changed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check whether two names refer to the same object

Use is to test object identity in a particular case:

x = []
y = x
z = []

print(x is y)  # True
print(x is z)  # False

id() can also report an object’s identity value during a program run, but use is for a direct identity comparison. Equality with == answers a different question: whether values compare as equal, not whether the names refer to the same object.

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.