📘 Lesson · Lesson 82
Deep Copy vs Shallow Copy
Two Kinds of Copy
💡 At a Glance
A shallow copy copies the outer object but shares inner objects. A deep copy copies everything independently.
Shallow Copy
Python
import copy a = [[1, 2], [3, 4]] b = copy.copy(a) # shallow b[0][0] = 99 print(a) # a also changed!
[[99, 2], [3, 4]]
Deep Copy
Python
import copy a = [[1, 2], [3, 4]] b = copy.deepcopy(a) # deep b[0][0] = 99 print(a) # a is safe
[[1, 2], [3, 4]]
Summary
- Shallow copy shares nested objects — changes affect both.
- Deep copy duplicates everything — fully independent.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.