Fundamentals 5 min read

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

This article explains the difference between assignment, shallow copy, and deep copy in Python, shows code examples, visual diagrams, and discusses why shallow copy is the default due to its speed, memory efficiency, and typical use cases.

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

1. Shallow and Deep Copy

In Python, assigning a variable creates a reference, not a copy; to avoid unintended modifications you need to create explicit copies of objects.

2. Assignment

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]

Note: l2 = l1 is just a reference, not a copy.

3. Shallow Copy

A shallow copy creates a new outer list, but inner objects still reference the original memory.

# Example 1: Adding element to outer 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: Modifying 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: Changing 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]]

4. Deep Copy

Deep copy recursively copies all mutable objects, creating independent copies.

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]]

Python optimizes deep copy by reusing immutable objects and recreating mutable ones.

5. Why Python defaults to shallow copy

Time: shallow copy is faster.

Space: uses less memory.

Efficiency: only copies top‑level references.

Conclusion

Immutable objects get new space on assignment.

Mutable objects share references unless copied.

Shallow copy copies only the first level; changes to nested mutable objects affect both copies.

Deep copy copies recursively until all nested mutable objects are duplicated.

Python provides several ways to shallow copy: copy.copy, list .copy(), slicing, etc.

In most cases, shallow copy is sufficient unless a true independent copy is required.

Advantages of shallow copy: fast, low memory usage, high 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.

Memory ManagementPythondeep 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.