Master Python Deep vs Shallow Copy: When and How to Use Them
This article explains why copying data in Python is necessary, distinguishes between assignment, shallow copy, and deep copy, demonstrates each with code examples and diagrams, and summarizes the performance and memory trade‑offs of the two copying methods.
In programming, passing data around can unintentionally modify the original value, so creating a copy is essential; Python provides both shallow and deep copy mechanisms.
1. Assignment vs. Copy
Assigning one variable to another (e.g., l2 = l1) creates a reference, not a copy, so changes to one affect the other.
l1 = [1, 2, 3, [22, 33]]
l2 = l1
l1.append(666)
print(l1) # [1, 2, 3, [22, 33], 666]
print(l2) # [1, 2, 3, [22, 33], 666]Diagram:
2. Shallow Copy
A shallow copy creates a new container object but does not recursively copy nested mutable objects; they still reference the original inner objects.
# Example 1: copy a large list
l1 = [1, 2, 3, [22, 33]]
l2 = l1.copy()
l1.append(666)
print(l1) # [1, 2, 3, [22, 33], 666]
print(l2) # [1, 2, 3, [22, 33]]
# Example 2: copy a small list and modify inner list
l1 = [1, 2, 3, [22, 33]]
l2 = l1.copy()
l1[-1].append(666)
print(l1) # [1, 2, 3, [22, 33, 666]]
print(l2) # [1, 2, 3, [22, 33, 666]]
# Example 3: change first element
l1 = [1, 2, 3, [22, 33]]
l2 = l1.copy()
l1[0] = 6
print(l1) # [6, 2, 3, [22, 33]]
print(l2) # [1, 2, 3, [22, 33]]Diagram:
3. Deep Copy
A deep copy recursively copies all nested mutable objects, creating completely independent structures.
import copy
l1 = [1, 2, 3, [22, 33]]
l2 = copy.deepcopy(l1)
l1.append(666)
print(l1) # [1, 2, 3, [22, 33], 666]
print(l2) # [1, 2, 3, [22, 33]]Diagram:
Python optimizes deep copy by recreating mutable objects in memory while reusing immutable objects, as shown in the following diagram:
Why is the default copy in Python shallow?
Time: Shallow copy is faster.
Space: Shallow copy uses less memory.
Efficiency: It copies only the top‑level references, making it generally more efficient than deep copy.
Summary
Immutable objects are copied by creating a new space during assignment.
Mutable objects share references on assignment; modifying one affects the other.
Both shallow and deep copy treat immutable objects like assignment (no new space).
Shallow copy duplicates only the first‑level references; changes to nested mutable objects affect the copy.
Deep copy recursively duplicates all nested mutable objects, leaving immutable objects shared.
Python offers multiple ways to perform shallow copy: copy.copy(), object’s .copy() method, slicing, etc.
In most cases, developers use shallow copy unless a specific need for deep copy arises.
Advantages of shallow copy: faster, less memory usage, higher efficiency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
