Python Variables: Why Changing One List Changes Another

 

Python Variables: Why Changing One List Changes Another





Have you ever written some Python code, changed one variable, and then watched in confusion as another variable changed too? 🤔

You're not alone. This is one of the first things that trips up new Python programmers. It might seem like a bug, but it's actually a designed feature of the language. Once you understand it, you'll not only avoid bugs but you'll feel much more confident.

Let's clear this up together!


It’s All About the Type of Data

The key to this mystery is that Python treats different types of data in different ways. For now, let's focus on the two you'll see most often:

  • Simple, Single Values: Like integers (5) and strings ("hello"). Let's call these "Rocks." You can't change a rock. If you want a different value, you just get a whole new rock.
  • Collections of Values: Like lists ([1, 2, 3]) and dictionaries ({"key": "value"}). Let's call these "Whiteboards." You can add to them, erase from them, and change them without getting a new one.

This difference explains everything.


Let's See It in Action

Example 1: Working with "Rocks" (Integers)

# Let's put a sticky note labeled 'a' on the number 10.
a = 10

# Let's put another sticky note labeled 'b' on whatever 'a' is stuck to.
b = a

# Now, let's move the sticky note 'a' to a new number, 5.
a = 5

# What are the values now?
print("a is", a) # Output: a is 5
print("b is", b) # Output: b is 10

What happened? We didn’t change the number 10. We just moved the tag a to a new number (5). The tag b was left behind, still stuck to 10. This is how numbers and strings always work.

Example 2: Working with a "Whiteboard" (List)

# Let's set up a whiteboard with [1, 2, 3] and point 'list_one' to it.
list_one = [1, 2, 3]

# Let's point another name, 'list_two', to the SAME whiteboard.
list_two = list_one

# Now, let's draw on the whiteboard (append a new number).
list_one.append(4)

# Let's see what each name shows us.
print("list_one is", list_one) # Output: list_one is [1, 2, 3, 4]
print("list_two is", list_two) # Output: list_two is [1, 2, 3, 4] 😲

What happened? Both list_one and list_two are just names for the same whiteboard. When we used list_one to append a 4, we drew directly on that shared whiteboard. So when we look at list_two, it shows the change too because it's looking at the exact same whiteboard!

This is the core of the issue. With lists, assignment (=) creates a new name for the same list, not a new list.


The #1 Fix: How to Actually Copy a List

So, what if you want to change a list without changing the original? You need to make a copy of the whiteboard first.

Here’s the common mistake and how to fix it.

The Mistake (What You Probably Did)

original_list = [1, 2, 3]
new_list = original_list # This is the mistake! This just makes a new name.
new_list.append(4)

print("The original list was changed:", original_list)
# Output: The original list was changed: [1, 2, 3, 4] ❌

The Solution (How to Do It Right)

You need to explicitly tell Python to make a copy. The easiest way is to use the .copy() method.

original_list = [1, 2, 3]
new_list = original_list.copy() # ✅ This makes a brand new copy!
new_list.append(4)

print("The original list is safe:", original_list)
# Output: The original list is safe: [1, 2, 3] ✅

print("The new list is changed:", new_list)
# Output: The new list is changed: [1, 2, 3, 4] ✅

Other ways to copy:

  • list(original_list): The list() function creates a new list from the contents of the original.
  • original_list[:]: This is a slicing trick that means "copy every item from start to finish."

They all do the same thing for simple lists. Just pick one you like best!


Your Cheat Sheet 🗒️

  • "Rocks" (Immutable): Numbers & Strings. Safe to assign with =. Changing one won't affect another.
  • "Whiteboards" (Mutable): Lists, Dictionaries, & Sets. Be careful! Using = just creates a new name for the same object.
  • Always use .copy() (or list() or [:]) if you want to change a list without changing the original.

This isn't a weird bug—it's a powerful feature of Python once you get used to it. Now you know the secret!


Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Insight: The Great Minimal OS Showdown—DietPi vs Raspberry Pi OS Lite

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison