Fundamentals 6 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Deep vs Shallow Copy: When and How to Use Them

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:

Assignment illustration
Assignment illustration

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:

Shallow copy illustration
Shallow copy illustration

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:

Deep copy illustration
Deep copy illustration

Python optimizes deep copy by recreating mutable objects in memory while reusing immutable objects, as shown in the following diagram:

Deep copy optimization
Deep copy optimization

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programming fundamentalsdeep copycopyshallow copy
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.