3. Lists, Tuples, Sets & References
How Variables & Lists Share Memory (References)
Solve one of Python's biggest beginner mysteries: why changing `b` leaves `a` untouched for numbers and strings, yet changes `a` when both point to the same list.
- Understand why b = a shares one list in memory for mutable types
- Compare object identity vs value equality using is vs ==
- Create safe independent copies with .copy() and copy.deepcopy()
Look at these two short Python snippets. Both use the exact same pattern (b = a), yet they behave completely differently!
Snippet A (Numbers):
a = 10
b = a
b = 99
print(a) # Prints: 10 (a stayed 10!)Snippet B (Lists):
a = [1, 2, 3]
b = a
b.append(99)
print(a) # Prints: [1, 2, 3, 99] <-- WAIT WHAT?! a changed too!This single behavior confuses almost every self-taught Python learner. Once you see why it happens, Python will suddenly make 10x more sense!
Remember "Sticky Notes on Values"
b = a (Shared List)
b = a.copy() (Separate Lists)
With b = a, both labels share one list. With b = a.copy(), Python creates a separate list in memory.
In Lesson 1, we learned that variables in Python are name tags pointing to values in memory (not physical separate boxes):
-
Numbers (
int,float), Booleans (bool), and Strings (str) are LOCKED (Immutable): When you writeb = 99, you cannot change10into99. Instead, Python creates a new number99and moves the sticky notebonto99. Meanwhileais still stuck onto10! -
Lists (and Dictionaries) are EDITABLE (Mutable): When you write
b = a, BOTH sticky notesaandbpoint to the exact same single list[1, 2, 3]in memory! Then when you runb.append(99), you didn't move the sticky noteb—you reached inside that shared list and added99to it! Soasees[1, 2, 3, 99]too!
Checking Whether Two Labels Share the Same Object (is vs ==)
==asks: "Do these two lists look equal (same values)?"isasks: "Are these two labels pointing to the exact same object in memory?"
x = [1, 2]
y = x # Same list object
z = [1, 2] # Brand-new separate list with the same values
print(x == z) # True (same contents)
print(x is y) # True (same object in memory!)
print(x is z) # False (two separate lists!)Making Real Copies (.copy() and deepcopy)
Whenever you want a separate list that you can edit safely without touching the original:
1. Flat Lists: Use .copy()
original = [10, 20, 30]
backup = original.copy()
backup.append(40)
print("Original:", original) # [10, 20, 30]
print("Backup:", backup) # [10, 20, 30, 40]2. Lists Inside Lists (Nested Lists): Use copy.deepcopy()
If a list contains another list inside it (like a game grid [[1, 2], [3, 4]]), .copy() only copies the outer list—the inner lists are still shared! Python's built-in copy module gives you deepcopy() to copy every layer cleanly:
import copy
grid = [[1, 2], [3, 4]]
safe_grid = copy.deepcopy(grid)
safe_grid[0].append(99)
print(grid) # [[1, 2], [3, 4]] (100% untouched!)
print(safe_grid) # [[1, 2, 99], [3, 4]]Later when you pass a list into a function (def add_item(cart): cart.append("Book")), the function receives a reference to that same list—so .append() modifies the caller's list too! Knowing references makes this crystal clear instead of mysterious.
Predict, Run & Fix
Predict what list_a will be before running, then fix list_b = list_a so the assert passes!
Click “Run Code” to execute this snippet right in your browser.
Quick Checkpoints
After running a = [5, 6]; b = a; b.pop(), what is a?
What is the difference between x == y and x is y?