Fundamentals 11 min read

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

This article explains Python's object model, the difference between mutable and immutable objects, how references work, and provides clear examples of shallow and deep copying—including when each method should be used to avoid unintended data modification.

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

Concept Overview

In everyday work data is passed around, which can be unintentionally modified. To avoid this, a copy is made; Python provides shallow and deep copy mechanisms.

Python Objects

Everything in Python is an object, including numbers, strings, functions, modules, etc. Each object has three attributes: identity, type, and value.

name = "laowang"
id(name)  # identity
type(name)  # type
name  # value

Mutable and Immutable Objects

Objects are divided into mutable (list, dict, set) and immutable (int, str, tuple). Mutable objects can change their value while keeping the same identity; immutable objects cannot be changed.

References

Each object occupies a memory address called a reference. Variable names are references to these addresses.

Reference Assignment

Assigning a variable makes multiple names refer to the same object. Changing a mutable object through one name affects all references.

a = 1
b = a
print(id(a))  # same as id(b)
a = 2  # a now refers to a new object; b still refers to the old one
print(id(a))
print(id(b))

Shallow Copy

A shallow copy creates a new top‑level object but copies only the references of nested objects. For immutable objects this is equivalent to assignment.

import copy
a = 10
b = copy.copy(a)
print(id(a) == id(b))  # True for immutable objects

For mutable objects a shallow copy shares inner references:

l1 = [1, 2, 3]
l2 = copy.copy(l1)
print(id(l1) == id(l2))  # True for the list object itself
l1[0] = 11
print(l1)  # [11, 2, 3]
print(l2)  # [11, 2, 3]  # both see the change because inner objects are shared

Deep Copy

A deep copy recursively copies all nested objects, so the new structure is completely independent.

import copy
l1 = [3, 4, [1, 2]]
l2 = copy.deepcopy(l1)
l1[2][0] = 11
print(l1)  # [3, 4, [11, 2]]
print(l2)  # [3, 4, [1, 2]]  # unchanged

Why Python Uses Shallow Copy by Default

Time: shallow copy is faster.

Memory: shallow copy uses less memory.

Efficiency: copying only the top level is usually sufficient.

Key Takeaways

Immutable objects create new space on assignment.

Mutable objects share the same reference when assigned.

Shallow copy copies only top‑level references; deep copy copies everything.

Python provides several ways to perform shallow copies (copy module, slice, factory methods).

In most cases shallow copy is enough; deep copy is needed for nested mutable structures.

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.

Pythondeep copyImmutablecopy moduleshallow copyreferenceMutable
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.