TGViewer
Python Learning Python Learning @python_bds Β· 5.74K subscribers
Post #1141 984
πŸš€ Shallow Copy vs Deep Copy in Python

When copying objects in Python, behavior changes for nested data structures.


πŸ“Œ Shallow Copy

A shallow copy creates a new outer object 
but inner objects remain shared references πŸ”— 

import copy

original = [[1, 2], [3, 4]]
shallow = copy.copy(original)

shallow[0].append(99)

print(original)
print(shallow)


Output:
[[1, 2, 99], [3, 4]]
[[1, 2, 99], [3, 4]]


πŸ‘‰ Nested objects are shared.


πŸ“Œ Deep Copy

A deep copy creates a completely independent copy 
including all nested objects 🧠 

import copy

original = [[1, 2], [3, 4]]
deep = copy.deepcopy(original)

deep[0].append(99)

print(original)
print(deep)


Output:
[[1, 2], [3, 4]]
[[1, 2, 99], [3, 4]]


πŸ‘‰ No shared references.


🎯 Quick Difference

β€’ Shallow β†’ Copies outer layer only 
β€’ Deep β†’ Copies entire structure 
β€’ Use deep copy for nested data
  • ❀ 4
More from @python_bds
  1. Oct 8, 2026πŸ› This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  2. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  3. Oct 4, 2026Python Set Methods ✍️
  4. Oct 3, 2026Post #1310
  5. Oct 1, 2026🐍 Python Beginner Notes
  6. Sep 30, 2026🧠 return vs print() in Python These are not interchangeable. def add(a, b): print(a + b)…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook β†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 β†’