Back to search

Nested lists and list copy behavior

Nested lists make shallow copies especially important because the outer list may copy while inner lists still alias the same objects.

Python Medium Theory

Nested lists and list copy behavior

Nested lists make shallow copies especially important because the outer list may copy while inner lists still alias the same objects.

  • Nested structures magnify aliasing bugs
  • list() or slicing makes a shallow copy
  • deepcopy is for fully independent nested state

Nested lists and list copy behavior