A deep copy creates an independent copy of a container and the modifiable objects nested inside it. Unlike a shallow copy, changing a nested list or dictionary in the copy does not change the original. section 5.6 section 7.2
Choose the copy technique according to the structure being copied:
- Use a full slice, +, *, or a container's copy method for a shallow copy when nested objects do not need independent copies. section 5.6 section 7.2
- Use copy.deepcopy when nested lists, dictionaries, or other modifiable objects must be changed without side effects on the original. section 5.6 section 7.2
Copying a nested list
import copy original = [[0], 1] shallow = original[:] deep = copy.deepcopy(original) shallow[0][0] = 'zero' # original becomes [['zero'], 1] deep[0][0] = 5 # original remains [['zero'], 1]




