Mutability vs Immutability

Same variable, different story depending on the type โ€” traced through id(), and the tricky case of a mutable list living inside an immutable tuple.

Overview

This topic covers what mutability means in Python, which data types are mutable vs immutable, and how that distinction plays out in practice โ€” including tricky edge cases involving nested data structures (a list inside a tuple, mixing + and in-place operations, etc.).

Key Concepts

Detailed Explanation

1. Definition of Mutability

Mutability refers to the ability to change or edit data in its memory location.
Mutability depends on the data type.
0x1000 | 4
0x1001 |
...
0x100n |

If a type is mutable, its value at a given memory address (e.g., 0x1000) can be edited directly. If it's immutable, that memory location's contents can never change โ€” any "update" requires creating a brand-new object elsewhere in memory.

2. Mutable vs Immutable Data Types

Immutable Data TypesMutable Data Types
StringList
intDictionary
FloatSet
Bool
Complex
Tuple
โš ๏ธ Note: Tuple sits in the immutable column โ€” but see the nested-structure section below for an important exception in behavior.

3. Immutable Types: "Changing" Actually Creates a New Object

Example: String

>>> a = "Hello"
>>> id(a)
1872263894720
>>> a = a + "world"
>>> a
'Helloworld'
>>> id(a)
1872264002992    # DIFFERENT id โ€” a brand-new string object was created

Example: Tuple

>>> T = (1, 2, 3)
>>> id(T)
1872263426336
>>> T = T + (5, 6)
>>> T
(1, 2, 3, 5, 6)
>>> id(T)
1872263312136    # DIFFERENT id โ€” a brand-new tuple object was created

Diagram (tuple concatenation creates a new object):

a = (1,2,3)  โ”€โ”€โ–บ  [0x1000 | (1,2,3) ]

a = a + (4,5)

a โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ  [0x1010 | (1,2,3,4,5) ]

The old tuple at 0x1000 is left behind; a now points somewhere new entirely.

โœ… Rule: For immutable types, any "modification" operation (+, +=, concatenation) always builds a new object and rebinds the variable name to it. The address changes.

4. Mutable Types: In-Place Modification (Same Memory Address)

Example: List with .append()

>>> L = [1, 2, 3]
>>> id(L)
1872263604680
>>> L.append(4)
>>> L
[1, 2, 3, 4]
>>> id(L)
1872263604680    # SAME id โ€” modified IN PLACE

Example: List Item Assignment

>>> L = [1, 2, 3]
>>> L[2] = 1
>>> L
[1, 2, 1]

The list object itself is mutated in place; its own id() doesn't change even though the value at index 2 changed.

โš ๏ธ Important distinction โ€” .append() vs +:

So even though List is a mutable type, using + (rather than an in-place method like .append()) still produces a new object โ€” mutability determines what's possible, not that every operation is automatically in-place.

5. Nested / Edge Cases: Mutable Object Inside an Immutable Container

A tuple is immutable โ€” you cannot reassign what object one of its slots points to. But if a slot holds a mutable object (like a list), that inner object can still be changed in place.

>>> a = [1, 2]
>>> b = [3, 4]
>>> c = (a, b)
>>> c
([1, 2], [3, 4])
>>> c[0][0] = 100      # mutating the LIST *inside* the tuple โ€” this works!
>>> c
([100, 2], [3, 4])
>>> id(c)              # the tuple's own id is UNCHANGED โ€” its structure never changed

Trying to reassign a tuple slot directly, however, fails:

>>> c[0] = [100, 2]
Traceback (most recent call last):
  ...
TypeError: 'tuple' object does not support item assignment
โœ… Key insight: A tuple's immutability means you can't change which objects it references (its slots are fixed). It says nothing about whether those referenced objects are themselves mutable. If a tuple contains a list, that list can still be freely modified โ€” the tuple's own identity/structure is untouched, but its content (as seen when you print it) can appear to change.

Important Points

Quick Revision