Introduction
When working with lists, dictionaries, or other complex objects in Python, you’ll often need to copy them. However, copying in Python isn’t always as simple as it looks. If you’re not careful, you might end up with unexpected behavior where changing one object also changes another.
This happens because Python offers two types of object copies:
In this article, we’ll break down the difference between shallow copy and deep copy in simple terms, with examples, a visual infographic, and a skill challenge to make learning fun and interactive.
What is a Shallow Copy?
A shallow copy creates a new object, but it only copies references of the inner objects (not the actual inner objects themselves).
This means changes to nested objects will still reflect in both the original and the copy.
Example:
import copy
# Original list with nested list
original = [[1, 2, 3], [4, 5, 6]]
# Shallow copy
shallow_copied = copy.copy(original)
# Modify nested list
shallow_copied[0][0] = 99
print("Original:", original)
print("Shallow Copy:", shallow_copied)
Output:
Original: [[99, 2, 3], [4, 5, 6]]
Shallow Copy: [[99, 2, 3], [4, 5, 6]]
Notice that modifying the shallow copy also changed the original. This is because only the outer list was copied, but the inner lists were still shared.
What is a Deep Copy?
A deep copy creates a new object and recursively copies all the nested objects inside it. This way, the new object is completely independent of the original.
👉 Changes in one object will not affect the other.
Example:
import copy
# Original list with nested list
original = [[1, 2, 3], [4, 5, 6]]
# Deep copy
deep_copied = copy.deepcopy(original)
# Modify nested list
deep_copied[0][0] = 99
print("Original:", original)
print("Deep Copy:", deep_copied)
Output:
Original: [[1, 2, 3], [4, 5, 6]]
Deep Copy: [[99, 2, 3], [4, 5, 6]]
Here, modifying the deep copy did not affect the original. That’s the power of deep copy.
Key Differences Between Shallow Copy and Deep Copy
Feature | Shallow Copy | Deep Copy |
---|
Copy Type | Copies only outer object | Copies outer and all nested objects |
Nested Objects | Still shared (changes affect both) | Fully independent (changes don’t affect each other) |
Speed | Faster (less memory overhead) | Slower (more memory used) |
Module Used | copy.copy() | copy.deepcopy() |
Visual Infographic: Shallow Copy vs Deep Copy vs Assignment
📌 Assignment (=
)
original = [1, 2, 3]
assigned = original
assigned[0] = 99
print(original) # [99, 2, 3]
📌 Shallow Copy (copy.copy()
)
Creates a new outer object.
Inner objects are still references (shared).
Changes in nested objects affect both.
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
shallow[0][0] = 99
print(original) # [[99, 2], [3, 4]]
Deep Copy (copy.deepcopy()
)
Creates a completely independent object.
Copies all nested objects recursively.
Changes do NOT affect the original.
import copy
original = [[1, 2], [3, 4]]
deep = copy.deepcopy(original)
deep[0][0] = 99
print(original) # [[1, 2], [3, 4]]
Summary Table
Operation | Independent Outer? | Independent Inner? |
---|
Assignment | No | No |
Shallow Copy | Yes | No |
Deep Copy | Yes | Yes |
Python Skill Challenge — Test What You Learned!
Now that you’ve learned about shallow vs deep copy in Python, it’s time to put your knowledge to the test. 🎉
👉 Take the Python Skill Challenge and check your skills. You’ll earn Sharp Tokens 🏆 as a reward for your knowledge!
👉 Don’t just read—apply your skills and get rewarded today!
Common Mistakes
Assuming =
(assignment) creates a copy → It does not. It only creates a reference.
Forgetting that copy.copy()
does not fully isolate nested objects.
Overusing deep copy for large structures → It can slow down performance unnecessarily.
Conclusion
Shallow Copy: Copies outer object only, inner objects are shared.
Deep Copy: Copies everything recursively, making objects fully independent.
Assignment: No copy at all, just a reference.
By understanding the difference, you can avoid unexpected bugs and make better decisions when handling complex data structures in Python.