Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Practical Pages

Mutable and immutable types in Python

Concept page on Python mutability: which built-in types can change in place, how aliasing and default arguments bite, and why tuples can hold mutable items.

An object is mutable if its contents can change after it is created, and immutable if they cannot. The distinction decides what happens when two names refer to the same object: with an immutable object nobody can change it behind your back; with a mutable one, every name sees every change.

Immutable does not mean the name is fixed. You can always rebind a name to a new object; what immutability forbids is changing the object itself.

Python
s = "abc"
s += "d"        # creates a NEW string and rebinds s
nums = [1, 2]
nums += [3]     # extends the SAME list in place

Try it

Python Which types change in place?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Aliasing: two names, one object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Classic traps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Built-in types

Immutable Mutable
int, float, complex, bool list
str, bytes dict
tuple, range set, bytearray
frozenset, None most instances of your own classes

Instances of user-defined classes are mutable unless you take steps: @dataclass(frozen=True), __slots__ with no setters, or properties without setters.

Usage notes

  • Hashing. Only hashable objects can be dict keys or set members. Immutable built-ins are hashable (a tuple only if all its items are); mutable containers are not. Use a tuple or frozenset when you need a composite key.
  • Methods that mutate return None. list.sort(), list.append(), dict.update() and set.add() change the object and return None, so x = x.sort() destroys your data. sorted() returns a new list.
  • += depends on the type. It calls __iadd__ when there is one (lists mutate in place), otherwise it falls back to a = a + b (tuples, strings and numbers build new objects).
  • Default arguments are evaluated once, at function definition time. Use None as the default for any mutable default.
  • Strings in loops. Repeated s += piece may be slow for long inputs; collect pieces in a list and call "".join(parts).
  • Immutability is shallow. A tuple only guarantees that it will always refer to the same items. See shallow and deep copies.
  • Safe sharing. Pass immutable values freely; copy mutable ones (list(x), x.copy()) when a function must not change the caller's data.