Python Shallow vs Deep Copy

Day 20 of My Python Learning Journey Today I learned about Shallow Copy and Deep Copy in Python. 📌 Shallow Copy A shallow copy creates a new object, but the nested objects inside it are still referenced from the original object. So, changes in nested elements will affect both copies. Example: import copy list1 = [[1,2,3],[4,5,6]] shallow = copy.copy(list1) shallow[0][0] = 100 print(list1) print(shallow) 📌 Deep Copy A deep copy creates a completely independent copy of the original object including all nested objects. Changes in one object will not affect the other. Example: import copy list1 = [[1,2,3],[4,5,6]] deep = copy.deepcopy(list1) deep[0][0] = 100 print(list1) print(deep) ✅ Key Difference Shallow Copy → Copies only the outer object Deep Copy → Copies outer object + all nested objects Learning these concepts helps in understanding memory handling and object references in Python #Python #PythonLearning #Day20 #CodingJourney #Programming #LearningEveryday

To view or add a comment, sign in

Explore content categories