What Is the Difference Between Mutable and Immutable Types in Python?
Learn the difference between mutable and immutable Python types, why it matters for dict keys and default arguments, with clear code examples.
Expected Interview Answer
Mutable types can be changed in place after creation (like lists, dicts, and sets), while immutable types cannot be altered once created (like ints, strings, and tuples) — any 'change' actually creates a new object.
Mutability affects identity and behavior: modifying a mutable object keeps the same `id()`, while an operation on an immutable object that looks like a mutation, such as string concatenation, produces a brand-new object. This matters for function arguments, since mutable default arguments are a classic bug (they persist across calls), for dictionary keys, which must be hashable and therefore immutable, and for thread safety, since immutable objects can be shared without locks.
- Immutables are safe to use as dict keys or set members
- Immutables are naturally thread-safe to share
- Mutable objects avoid copying overhead for in-place updates
- Understanding this prevents the mutable-default-argument bug
- Clarifies why `id()` changes or stays the same after an operation
AI Mentor Explanation
A mutable type is like a scorecard that gets updated ball by ball — the same physical card, runs added directly onto it. An immutable type is like a printed match result certificate: once issued for a completed game it cannot be edited, so recording a correction means issuing an entirely new certificate rather than scribbling on the old one.
Step-by-Step Explanation
Step 1
Mutable examples
list, dict, and set can be modified in place — same id() before and after.
Step 2
Immutable examples
int, float, str, tuple, and frozenset cannot change; operations create new objects.
Step 3
Identity check
Use id() to confirm: mutation keeps id() constant, immutability changes id() on 'update'.
Step 4
Hashability link
Only immutable, hashable objects can be dict keys or set members.
Step 5
The default-argument trap
A mutable default argument (e.g. def f(x=[])) is created once and shared across all calls, causing bugs.
What Interviewer Expects
- Lists correct mutable and immutable built-in types
- Explains that immutability doesn't prevent rebinding a variable
- Connects immutability to hashability and dict keys
- Can explain the mutable default argument bug
- Uses id() or is to demonstrate identity vs equality
Common Mistakes
- Thinking tuples are mutable because they can contain mutable elements
- Confusing rebinding a variable with mutating an object
- Using a mutable default argument in a function signature
- Assuming strings can be modified in place
Best Answer (HR Friendly)
“Mutable types, like lists, can be changed after you create them, while immutable types, like strings or tuples, cannot — any change actually creates a brand-new value behind the scenes. This distinction matters for writing predictable code and avoiding subtle bugs.”
Code Example
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4] -- same list, mutated
s = "hi"
t = s
t += " there"
print(s, t) # 'hi' 'hi there' -- new string created
# Mutable default argument bug
def append_item(item, bucket=[]):
bucket.append(item)
return bucket
print(append_item(1)) # [1]
print(append_item(2)) # [1, 2] -- surprise! same default list reusedFollow-up Questions
- Why can't a list be used as a dictionary key?
- What is the mutable default argument bug and how do you avoid it?
- Are tuples always fully immutable?
- How does copy.deepcopy differ from a shallow copy for mutable objects?
- How does immutability help with thread safety?
MCQ Practice
1. Which of these is immutable?
Tuples are immutable; their contents cannot be reassigned after creation.
2. Why must dictionary keys be immutable?
Dict keys must be hashable, and mutable objects can't safely provide a stable hash since their contents can change.
3. What causes the mutable default argument bug?
Default argument values are evaluated once at function definition time, so a mutable default persists and is shared across calls.
Flash Cards
Name three mutable built-in types. — list, dict, set.
Name three immutable built-in types. — int, str, tuple.
Why must dict keys be immutable? — They must be hashable, and mutable objects can't guarantee a stable hash.
What's the mutable default argument bug? — A mutable default value is created once and shared across all calls.