Two teams need independent copies of a checklist with sublists.
What You Need to Understand
A shallow copy duplicates only the outer collection, while a deep copy recursively copies nested objects where possible. This distinction matters when a data structure includes lists within lists or dictionaries.
When to Use It
Two teams need independent copies of a checklist with sublists.
Step-by-Step Python Example
import copy
original = [["draft"], ["review"]]
shallow = original.copy()
deep = copy.deepcopy(original)
original[0].append("approved")
print(shallow[0])
print(deep[0])
How the Example Works
The shallow copy shares the inner list and sees approved. The deep copy keeps an independent inner list.
Example Output
['draft', 'approved']
['draft']
False
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
import copy
records = [{"done": False}]
clone = copy.deepcopy(records)
records[0]["done"] = True
print(clone[0]["done"])
Important Behavior and Edge Cases
Deep copying can be expensive and may not be meaningful for open files, connections or other resource objects. Prefer simpler data design when possible.
Try It Yourself
Create two lists of dictionaries and compare changes after shallow and deep copying.
Common Beginner Mistakes
- Deep copying can be expensive and may not be meaningful for open files, connections or other resource objects. Prefer simpler data design when possible.
- Overlooking how an iterable is consumed, how element positions are chosen, or whether a function creates a new list.
Key Takeaways
- A shallow copy duplicates only the outer collection, while a deep copy recursively copies nested objects where possible.
- The shallow copy shares the inner list and sees
approved. - Deep copying can be expensive and may not be meaningful for open files, connections or other resource objects.