Problem Statement
Explain shallow copy versus deep copy for nested structures with an example.
Explanation
A shallow copy duplicates the outer container but shares references to inner objects. Changing an inner list in the copy also changes it in the original, because both point to the same inner object.
A deep copy recursively copies inner objects, so edits in the copy do not affect the original. Use shallow copy when inner items are immutable or you want sharing; use deep copy when you need isolation between complex nested graphs.
Code Solution
SolutionRead Only
import copy orig=[[1],[2]] sh = copy.copy(orig) dp = copy.deepcopy(orig) sh[0][0]=9 # orig changes # dp edits do not affect orig