Shallow Copy Vs Deep Copy in Python (Krish Naik)
Three Operation in Focus
=copy()deepcopy()
1. = (Assignment ) - Same Reference
Section titled “1. = (Assignment ) - Same Reference”lst1 = [1, 2, 3, 4]lst2 = lst1 # Not a copy, just another reference
lst2[1] = 1000 # Change through lst2print(lst1) # [1, 1000, 3, 4] — also changedlst2 = lst1means both point to the same object.- Any change through
lst2affectslst1.
2. copy() - Shallow Copy
Section titled “2. copy() - Shallow Copy”lst1 = [1, 2, 3, 4]lst2 = lst1.copy() # Creates a new list
lst2[1] = 1000print(lst1) # [1, 2, 3, 4] — unchangedcopy()creates a new list with same elements (primitives).- So, changing one does not affect the other.
Shallow Copy with Nested Lists
Section titled “Shallow Copy with Nested Lists”lst1 = [[1, 2, 3, 4], [5, 6, 7, 8]]lst2 = lst1.copy()Case 1: Replace whole nested list
lst2[1] = [100, 200, 300, 400]print(lst1) # [[1, 2, 3, 4], [5, 6, 7, 8]] — unchanged- Replacing whole inner list creates a new object → doesn’t affect original.
**Case 2: Append new nested list ⭐
lst1.append([2, 3, 4, 5])
print(lst1) # [[1, 2, 3, 4], [5, 6, 7, 8], [2, 3, 4, 5]]print(lst2) # [[1, 2, 3, 4], [5, 6, 7, 8]] — unchanged- Adding new outer element → doesn’t affect original.
Case 3: Modify inner element
lst2[1][0] = 100print(lst1) # [[1, 2, 3, 4], [100, 6, 7, 8]] — changed!- Because
.copy()only copies the outer list, not the inner ones. - Both
lst1[1]andlst2[1]still refer to same inner list.
3. deepcopy() - Deep Copy
Section titled “3. deepcopy() - Deep Copy”import copylst1 = [1, 2, 3, 4]lst2 = copy.deepcopy(lst1)
lst2[1] = 100print(lst1) # [1, 2, 3, 4] - unchanged- In Non-Nested Object -> Shallow Copy = Deep Copy
Deep Copy with Nested Lists
Section titled “Deep Copy with Nested Lists”lst1 = [[1, 2, 3], [3, 4, 5], [5, 6, 7]]lst2 = copy.deepcopy(lst1)
lst2[1][0] = 100print(lst1) = # [[1, 2, 3], [3, 4, 5], [5, 6, 7]] - unchangeddeepcopy()creates new copies of all inner objects (lists inside list).- So changes in
lst2won’t affectlst1, even in nested levels. - This is different from
.copy()which only copies the outer list and shares inner ones.