Sitemap

Member-only story

Shallow Copy vs Deep Copy: Key Differences

4 min readJul 16, 2025
Press enter or click to view image in full size

What is a Shallow Copy?

A shallow copy creates a new object, but inserts references to the objects found in the original. The copied object shares the same nested objects with the original object.

# Python example of shallow copy
import copy
original = [[1, 2, 3], [4, 5, 6]]
shallow = copy.copy(original) # or original.copy() for lists
# Modifying nested object affects both
shallow[0][0] = 'X'
print(original) # [['X', 2, 3], [4, 5, 6]] - original is affected!
print(shallow) # [['X', 2, 3], [4, 5, 6]]

What is a Deep Copy?

A deep copy creates a new object and recursively copies all nested objects. The copied object is completely independent of the original.

# Python example of deep copy
import copy
original = [[1, 2, 3], [4, 5, 6]]
deep = copy.deepcopy(original)
# Modifying nested object only affects the copy
deep[0][0] = 'X'
print(original) # [[1, 2, 3], [4, 5, 6]] - original unchanged!
print(deep) # [['X', 2, 3], [4, 5, 6]]

Key Differences

1. Memory Allocation

Shallow Copy:

// JavaScript example
const…

--

--

Aditya Bhuyan
Aditya Bhuyan

Written by Aditya Bhuyan

I am Aditya. I work as a cloud native specialist and consultant. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer.