Deepcopy and Shallow copy

  • In case of shallow copy, a reference of object is copied in other object.
    • It means that any changes made to the copied object do reflect in the original object
    • External object id changes but Internal object id remains same
  • In case of deep copy, a copy of object is copied in other object.
    • It means that any changes made to the copied object do not reflect in the original object
    • External and internal object ids changes

Generally used

[:] and copy() –> both does shallow copy

l1 = [0, 2, 4, 6, 8]

l2 = l1
l1 is l2 # True
# i.e id(l1) == id(l2)

l3 = l2 [:]
l3 is l2  # False
# i.e id(l2) != id(l3)

l4 = l2.copy()
l4 is l2 # False
# i.e id(l2) != id(l4)

Deepcopy and shallow copy

copy.copy() –> import copy

import copy

l5 = [1, 2, [4, 5, 7], 8]
l6 = copy.copy(l5) 
print(l5)

l5[2][2] = 9
l5[3] = 10
print(l5)
print(l6)

# OP
# [1, 2, [4, 5, 7], 8]
# [1, 2, [4, 5, 9], 10]
# [1, 2, [4, 5, 9], 8]
# NOTE:
  * l5: Changed made --> Int and ext obj elements
  * l6: Int obj element changes, ext obj element remains same

print(l5 is l6)
# False
print(l5[2] is l6[2])
# True  -->  internal list point to same obj

copy.deepcopy()

l7 = [1, 2, [4, 5, 8], 11]
l8 = copy.deepcopy(l7)
print(l7 is l8)
print(l7)
l7[2][2] = 9
l7[3] = 10

print(l7)
print(l8)

# False
# [1, 2, [4, 5, 8], 11]
# [1, 2, [4, 5, 9], 10] 
# [1, 2, [4, 5, 8], 11]
# NOTE:
  * l7: On int and ext obj's element change
  * l8: Nothing changes

print(l7 is l8)
# False
print(l7[2] is l8[2])
# False

Reference