Understanding Shallow and Deep Copy in Python
This article explains Python's shallow and deep copy mechanisms, demonstrates their differences with code examples, visual diagrams, and discusses when to use each approach, highlighting performance and memory considerations in software development.
When data is passed between functions or components, unintended modifications can occur; creating a copy of the data prevents changes to the original. This article introduces Python's shallow and deep copy techniques to address that need.
1. Shallow Copy
Assigning one variable to another (e.g., l2 = l1 ) creates a reference, not a copy, so modifications affect both variables.
<code>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]</code>Using list.copy() (or copy.copy() ) performs a shallow copy: a new list object is created, but its elements still reference the original objects.
<code>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]]</code>Because the inner list is mutable, changes to it affect both copies, as shown in the subsequent examples.
2. Deep Copy
A deep copy creates a completely independent object hierarchy, duplicating mutable objects recursively.
<code>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]]</code>Python optimizes deep copies by reusing immutable objects (e.g., numbers, strings) while recreating mutable ones, reducing memory usage.
3. Why Python Uses Shallow Copy by Default
Time: Shallow copy is faster because it copies only the top‑level references.
Space: It consumes less memory since inner objects are shared.
Efficiency: For most use‑cases, copying only the first level is sufficient and more performant.
Summary
Immutable objects are effectively copied by assignment.
Mutable objects share references when assigned; changes affect all references.
Shallow copy duplicates the outer container but keeps inner references.
Deep copy recursively duplicates containers, creating independent mutable objects.
Python provides multiple shallow‑copy methods: list.copy() , copy.copy() , slicing, etc.
In practice, shallow copy is used most of the time unless a full independent copy is required.
Shallow copy offers speed and lower memory consumption; deep copy ensures complete isolation.
Images illustrating the memory layout and copy behavior are included in the original article.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.