What is the difference between a shallow copy and a deep copy in Python?
Quick answer
A shallow copy creates a new container but reuses the same nested objects, while a deep copy recursively copies everything so the new object is fully independent.
With a shallow copy (copy.copy(), list.copy(), slicing items[:]), changing a nested list in the copy also changes it in the original, because both point to the same inner object. copy.deepcopy() duplicates the nested objects as well, handling circular references, but it is slower and uses more memory.
Plain assignment b = a does not copy at all: both names refer to the same object.
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)
original[0].append(99)
print(shallow[0]) # [1, 2, 99] - shared inner list
print(deep[0]) # [1, 2] - independent