Copying Variables

In Python, when we assign one variable to another, we’re not really creating a new copy of the object — we’re simply making a new reference that points to the same object in memory.
This means that if we change one variable, the other one may also change — because both of them are pointing to the same data!
Let’s explore how this works, and how we can correctly copy objects in Python.
Example 1:
When you assign one variable to another, like this:
x = [1, 2, 3]
y = x # y references the same list as x
You might think you’ve created a new copy, but you haven’t
Let’s check:
x = [1, 2, 3]
y = x
y.append(4) # Add an element to y
print("x:", x)
print("y:", y)
Output:
x: [1, 2, 3, 4]
y: [1, 2, 3, 4]
- Both x and y point to the same list in memory.
- When you modify one, the other changes too!
You can check if they share the same memory using the is
operator:
print(x is y) # True
Example 2:
If you want to make a separate copy of your list (or other mutable object), use copying methods to do so. Python provides several ways to do this.
- Using
list()
orSlicing
x = [1, 2, 3]
y = list(x) # or y = x[:] ← slicing creates a shallow copy
y.append(4)
print("x:", x)
print("y:", y)
Output:
x: [1, 2, 3]
y: [1, 2, 3, 4]
They are now independent lists
Check their identity by:
print(x is y) # False
Example 3:
Python provides a built-in module called copy
that helps you make shallow and deep copies. Let’s import it and see how it works.
import copy
x = [1, 2, 3]
y = copy.copy(x) # Makes a shallow copy
y.append(4)
print("x:", x)
print("y:", y)
x
and y
are now separate lists
Example 4:
To find the difference between them. Let’s look at an example using nested lists (a list inside another list):
import copy
x = [[1, 2], [3, 4]]
y = copy.copy(x) # Shallow copy
Let’s check what happens if we modify one of the inner lists:
y[0].append(99)
print("x:", x)
print("y:", y)
Output:
x: [[1, 2, 99], [3, 4]]
y: [[1, 2, 99], [3, 4]]
Both variables changed, because copy.copy()
only makes a shallow copy. It creates a new outer list, but the inner lists are still shared between the two variables.
Example 5:
Use deep copy to completely clone every level of your object (including nested lists or dictionaries).
import copy
x = [[1, 2], [3, 4]]
y = copy.deepcopy(x)
y[0].append(99)
print("x:", x)
print("y:", y)
Output:
x: [[1, 2], [3, 4]]
y: [[1, 2, 99], [3, 4]]
The two lists are totally independent now.
Try It Yourself
- Create a list with numbers or strings.
- Try copying it using copy.copy() and copy.deepcopy().
- Modify one and print both lists to see the difference.
Example solution:
import copy
data = [[10, 20], [30, 40]]
shallow = copy.copy(data)
deep = copy.deepcopy(data)
shallow[0].append(50)
deep[1].append(60)
print("Original:", data)
print("Shallow:", shallow)
print("Deep:", deep)
Key Takeaways
- When you assign one variable to another, you’re not creating a copy — you’re just sharing the same object.
- Use copy.copy() for shallow copies.
- Use copy.deepcopy() for full independent duplicates.
- Always double-check with id() or is to confirm if two variables share memory.
- Python automatically manages memory — but understanding how references work helps you write safer and cleaner code.
💬 : When working with complex data structures (like lists of lists or nested dictionaries), prefer deepcopy to avoid unexpected data changes.
Happing Reading!