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.

3 min readUpdated 2026-11-29
In This Lesson
  • 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):

python
a = 10
b = a
b = 99
print(a)   # Prints: 10  (a stayed 10!)

Snippet B (Lists):

python
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"

Shared Reference vs .copy()

b = a (Shared List)

ab
[1, 2, 3, 99]

b = a.copy() (Separate Lists)

a[1, 2, 3]
b[1, 2, 3, 99]

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 write b = 99, you cannot change 10 into 99. Instead, Python creates a new number 99 and moves the sticky note b onto 99. Meanwhile a is still stuck onto 10!

  • Lists (and Dictionaries) are EDITABLE (Mutable): When you write b = a, BOTH sticky notes a and b point to the exact same single list [1, 2, 3] in memory! Then when you run b.append(99), you didn't move the sticky note b—you reached inside that shared list and added 99 to it! So a sees [1, 2, 3, 99] too!

Checking Whether Two Labels Share the Same Object (is vs ==)

  • == asks: "Do these two lists look equal (same values)?"
  • is asks: "Are these two labels pointing to the exact same object in memory?"
python
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()

python
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:

python
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]]
Why Passing a List Into a Function Can Change It

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!

Python 3Try It Live: Fix the Shared Reference Bug
Output

Click “Run Code” to execute this snippet right in your browser.

Quick Checkpoints

Check Your Understanding

After running a = [5, 6]; b = a; b.pop(), what is a?

Check Your Understanding

What is the difference between x == y and x is y?