Programming

What is the difference between deep copy and shallow copy in Python?

Short answer

A shallow copy creates a new object but still references the same nested objects as the original, while a deep copy recursively copies everything, so the new object is fully independent.

This distinction only matters for objects that contain other objects, like a list of lists — copying simple, flat data (like a list of numbers) behaves the same either way.

Shallow copy

import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
shallow[0][0] = 99
print(original)  # [[99, 2], [3, 4]] — original changed too!

The outer list is new, but the inner lists inside it are still the same shared objects as in the original.

Deep copy

deep = copy.deepcopy(original)
deep[0][0] = 99
print(original)  # unchanged — fully independent copy

A deep copy recursively duplicates every nested object, so modifying the copy never affects the original, no matter how deeply nested the data is. Deep copies use more memory and take longer, so shallow copies are preferred when the data structure doesn't contain nested mutable objects.

Last reviewed: September 2026