PyCodeItPython trace & execution models
Python Mastery Hub/tuples immutability
Python 3.12+ CPython TrackPyodide WASM Sandbox

Tuples Immutability & Memory Layout

Master tuple immutability, nested mutability, hashing constraints, memory optimization, and structural unpacking traps.

How Tuples Immutability & Memory Layout behaves during execution

Tuples (PyTupleObject) are fixed-length, immutable sequences of object references. In CPython, tuples provide structural memory optimization through free-list recycling and pointer caching. Because tuples cannot be resized or reassigned after creation, they are hashable (provided all contained elements are hashable), allowing them to serve as dictionary keys and set members.

Reference Immutability vs Underlying Object Mutability

Immutability in Python applies strictly to the tuple's pointer array: once constructed, the sequence of memory addresses stored in the PyTupleObject cannot be altered. However, if a tuple contains references to mutable objects (like lists or dictionaries), those underlying objects can still be mutated in place.

The In-Place Addition on Tuple Elements Trap (t[0] += [1])

Executing t[0] += [1] on a tuple containing a list (t = ([1, 2],)) mutates the list in place, but then raises a TypeError when attempting to assign the result back to t[0]. The list is mutated to [1, 2, 1], yet an exception is raised because the tuple's pointer slot cannot be rebound. Immutability guarantees pointer stability, not deep object immutability.

How to use this tool

Read the server-rendered examples above first, write down each variable change in a trace table, then use the interactive workspace below to step through execution. Tracing builds clear mental models for technical interviews.

Worked example

# Trace the execution line by line
x = 10
y = x * 2
print(x + y)

Static result: 30

Controls

The code panel shows the snippet; Run executes it in the browser Pyodide sandbox; Next and Previous step through line states; the result panel displays output and state changes.

Honest limits

Python code executes locally inside your browser WebAssembly (Pyodide) sandbox. Execution is client-side, rate-free, and isolated from our servers. Do not enter credentials or private API keys.

Continue with the Python Mastery Hub.

Interactive Dry-Run Execution Workspace

Problem 1 / 61+50 XP

Modifying a Mutable Element Inside a Tuple

hard
Task: Output Execution Prediction

Trace CPython execution step-by-step and deduce the exact stdout string emitted by this snippet.

Socratic Guidance-5 XP penalty per revealed hint
Topic Problems0 / 61 Solved
main.pyCPython 3.12 (WASM)
1
2
3
t = (1, [2, 3])
t[1].append(4)
print(t)
Press Enter to submit
Match exact whitespace and capitalization

Frequently Asked Questions on Tuples Immutability & Memory Layout

Why does t[0] += [1] both mutate the list AND raise an exception in a tuple?

+= calls __iadd__ on the inner list, mutating it in place. Then Python attempts to execute the assignment operator back to t[0], which triggers a TypeError because tuple items cannot be assigned. The mutation succeeds before the assignment fails.

When is a tuple hashable and usable as a dictionary key?

A tuple is hashable if and only if every element it contains is also hashable. A tuple containing only integers and strings is hashable, but a tuple containing a list or dictionary raises TypeError: unhashable type.

How do tuples achieve memory efficiency over lists in CPython?

Tuples have fixed length, so CPython does not over-allocate extra pointer capacity. Additionally, CPython caches small, deallocated tuple structs in a free list, reducing heap allocation overhead.