What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
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
In Python, mutable objects can change after they are created; immutable objects cannot. Assignment does not copy an object: it binds a name to it. So if two names refer to the same mutable object, a change through either name is visible through both.
What mutable and immutable mean
Mutability describes whether an existing object’s state can change. Python’s glossary defines an immutable object as “an object with a fixed value.” A mutable object can be changed in place; for an immutable object, producing a different value means creating or obtaining another object and binding a name to it.
Common mutable built-ins include lists, dictionaries, sets, and bytearrays. Common immutable built-ins include strings, bytes, numeric values, and tuples; frozenset is also immutable. These categories describe the objects, not the variable names that refer to them.
Does assigning a variable copy the object?
No. Assignment creates a binding between a name and an object; it does not make an independent copy. If two names refer to one mutable object, either name can be used to change that object.
#1 Best Overall
items = [1, 2]
alias = items
alias.append(3)
print(items) # [1, 2, 3]
print(alias) # [1, 2, 3]
alias = items did not create a second list. Both names refer to the same list, so appending through alias changes what is seen through items.
What happens when you work with an immutable value?
A string cannot be edited in place. String operations produce a value, and assignment can bind a name to that result without changing the original string.
word = "python"
new_word = word.upper()
print(word) # python
print(new_word) # PYTHON
The same distinction applies to concatenation: text = text + "s" binds text to the concatenated string value; it does not append a character to the existing string object. The Python FAQ explains string immutability partly in terms of allocating storage for fixed requirements and treating strings as elemental values, like numbers. That is a rationale, not a claim of a measured performance gain.
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 glitchesRank #2
Can a tuple contain a mutable object?
Yes. A tuple is an immutable sequence: its element references cannot be reassigned after creation. But an element can refer to a separate mutable object, and that object can still change.
record = (["draft"], "notes")
record[0].append("published")
print(record) # (['draft', 'published'], 'notes')
The tuple still refers to the same list in its first slot; the list’s contents changed. Trying to replace that slot, as in record[0] = ["published"], raises TypeError.
A subtler case combines a mutable operation with tuple assignment:
t = (["foo"], "bar")
t[0] += ["item"]
The list’s in-place addition extends it. Python then attempts to assign the result back to tuple slot zero, which is not allowed, so the statement raises TypeError even though the list has already been extended. This is why “the tuple is immutable” does not mean that every object reachable through it is immutable.
When should you make a copy?
Copy when you need an independent container rather than another name for the same one. The level of independence matters: a shallow copy creates a new outer container but shares references to objects inside it. A deep copy recursively copies contained objects according to the behavior of Python’s copy module.
import copy
original = [[1], [2]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)
original[0].append(9)
print(shallow) # [[1, 9], [2]]
print(deep) # [[1], [2]]
shallow has its own outer list, but its first inner list is shared with original. deep has recursively copied the nested lists in this example. For a flat list, original.copy() is commonly enough when you only need a separate outer list.
How does mutability relate to hashability?
Hashability determines whether an object can be used as a dictionary key or set member. A hashable object has a hash value that remains stable; dictionaries and sets rely on it. Many immutable built-ins are hashable, while mutable containers such as lists and dictionaries are generally unhashable.
Do not assume that every immutable object is hashable. A tuple is hashable only when all its elements are hashable; a tuple containing a list cannot be used as a dictionary key. A frozenset is hashable only when its elements are hashable. User-defined classes can also customize equality and hashing, so their behavior depends on their implementation.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Quick comparison
| Question | Mutable object | Immutable object |
|---|---|---|
| Can its existing state change in place? | Yes. For example, a list can append an element. | No. A tuple cannot have one of its element references replaced. |
| Does assigning it to another name make a copy? | No. The new name refers to the same object unless you explicitly copy. | No. Assignment still binds a name; it does not copy. |
| Can it be a dictionary key or set member? | Mutable containers such as lists and dictionaries are generally unhashable. | Often, but not always. A tuple or frozenset is hashable only if its contents are hashable. |
| What does a shallow copy share? | A new outer container, with references to the same contained objects. | Copying is usually unnecessary to protect the immutable value itself; referenced mutable objects still require attention. |
How to check whether two names refer to the same object
Use is to test object identity, not ==. Equality checks whether values compare equal; identity checks whether the names refer to the very same object. This distinction is especially useful when diagnosing aliasing with mutable values.
Best Value
items = [1, 2]
alias = items
separate = items.copy()
print(alias is items) # True
print(separate is items) # False
For immutable values, avoid inferring mutability or identity from whether Python happens to reuse an object. The relevant distinction is whether an operation can change the existing object, not whether two expressions print the same value or happen to refer to the same object.
Official Python references
- Python glossary: immutable and hashable
- Python data model: object behavior and immutable sequences
- Python copy module: shallow and deep copies
- Python programming FAQ: Why are Python strings immutable?
- Python data structures tutorial: lists and dictionary keys
These concepts are stable across Python versions; consult the documentation for the version you use for version-specific behavior or API details.
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.

