KITFORMA · EDITORIAL GUIDE
Why does changing a nested list also change its shallow copy?
KitForma editorial guide. A shallow copy duplicates the outer container while retaining references to its children. `copy = original[:]` does not isolate nested lists.
Step-by-step guidance
copy = original[:] does not isolate nested lists.
Use copy.deepcopy for an independent object graph when that matches the model, or construct exactly the nested values that should be independent. In a test, compare copy is original and copy[0] is original[0] separately, then mutate one child. Deep copying is not a universal snapshot mechanism: open files, network clients and deliberately shared objects require explicit handling. Prefer immutable records for data that should be shared safely, and do not use copying to hide unclear ownership.
Minimal example:
from copy import deepcopy
original = [[1]]
shallow = original[:]
independent = deepcopy(original)
shallow[0].append(2)
assert original == [[1, 2]]
assert independent == [[1]]Sources and verification
Sources checked:
Scope: This editorial guide is based on the cited sources and tool behavior. A forum question or a query observed for our site does not establish market search volume, low competition, guaranteed rankings or inadequate answers elsewhere.
This starter guide was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.