In Python, mutable objects can be changed in place (same object, different content); immutable objects cannot—any “change” creates a new object. This distinction affects dictionary keys, default arguments, and whether two names share the same data. This article summarizes which built-in types are mutable or immutable and why it matters. See also Lists vs tuples and Shallow vs deep copy.
Simple explanation
Mutable: you can alter the object (add, remove, or change elements) without creating a new one. Immutable: the object’s value cannot change; operations that would “change” it produce a new object instead.
Mutable types
These can be modified in place:
list— append, remove, sort, assign to indexdict— add/delete keys, change valuesset— add, remove (no index assignment)
my_list = [1, 2, 3]
my_list.append(4) # OK
my_list[0] = 10 # OK
print(my_list) # [10, 2, 3, 4]
Immutable types
You cannot change their value; “changes” create new objects:
int,float,boolstr— no item assignment; “modification” is a new stringtuple— no item assignment, no append/removefrozenset— immutable set
s = "hello"
# s[0] = "H" # TypeError
# Create new string instead:
s = "H" + s[1:]
print(s) # Hello
Why it matters: dictionary keys
Dictionary keys must be hashable and immutable. If a key could change, its hash could change and the dict would break. So lists and sets cannot be keys; tuples and strings can. See Dictionaries and Lists vs tuples.
key = (1, 2, 3)
d = {key: "value"} # OK
# d[[1,2,3]] = "value" # TypeError: unhashable type: 'list'
Assignment vs in-place modification
Assigning to a variable rebinds the name; it does not change the old object. For immutables, “change” is always rebinding (e.g. x = x + 1). For mutables, in-place methods (e.g. lst.append(4)) change the same object, so other references see the change:
# Immutable: rebinding
x = 5
y = x
x = 10
print(y) # 5
# Mutable: same object
a = [1, 2, 3]
b = a
a.append(4)
print(b) # [1, 2, 3, 4] — b and a refer to the same list
Next steps: Core Concepts · Lists vs tuples · Shallow vs deep copy · Data types