Understanding Shallow and Deep Copy in Python
This article explains how Python variables reference objects in memory, distinguishes between shallow and deep copying, demonstrates three common copying techniques (slicing, factory functions, and the copy method) with lists and tuples, and shows how to use the copy module for deep copies.
To grasp Python's shallow and deep copy mechanisms, first understand that a variable consists of a type, a name, and a value, and that every value is an object stored in memory with the variable name acting as a reference (similar to a pointer).
An assignment such as a = 'Python' creates a reference from a to the string object.
When another variable is assigned with b = a , both a and b point to the same object, as shown by identical id() values; reassigning a breaks this link while b remains unchanged.
Shallow copy copies only the top‑level container, i.e., it copies the reference to the inner objects, whereas deep copy recursively copies all nested objects, producing independent copies.
For non‑nested or immutable objects (numbers, strings, booleans) shallow and deep copy behave the same, but for mutable containers like lists or dictionaries the difference becomes significant.
Python provides three common ways to copy a container:
Slice operation [:]
Factory functions such as list() , set() , dict()
The copy() method from the copy module
Example using slicing on a list:
<code>>> temp = [1,2,[3,4],5]
>>> temp2 = temp[:]
>>> temp[0] = 6
>>> temp[2][1] = 7
>>> temp
[6,2,[3,7],5]
>>> temp2
[1,2,[3,4],5]</code>Only the outer list is copied; the inner list remains a shared reference, so modifications to the inner list affect both temp and temp2 .
Using a factory function:
<code>>> temp = [1,2,[3,4],5]
>>> temp2 = list(temp)
>>> id(temp) != id(temp2)
>>> id(temp[2]) == id(temp2[2])</code>Tuples are immutable; converting a list to a tuple with tuple() creates a new outer object, but the inner mutable elements still share references.
The copy() method behaves like slicing, copying only the outer container.
To avoid shared references, use deep copy from the standard library:
<code>import copy
new_obj = copy.deepcopy(original_obj)</code>After a deep copy, none of the mutable elements share the same memory address, so changes to one object do not affect the other.
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.