Question 22 (Interview-Level):
Explain the difference between
Options:
A) Both modify the original list
B)
C) Shallow copy affects nested objects, deepcopy doesn't
D)
#Python #Interview #DeepCopy #MemoryManagement
✅ By: https://yangx.top/DataScienceQ
Explain the difference between
deepcopy
and regular assignment (=
) in Python with a practical example. Then modify the example to show how deepcopy
solves the problem. import copy
# Original Problem
original = [[1, 2], [3, 4]]
shallow_copy = original.copy()
shallow_copy[0][0] = 99
print(original) # What happens here?
# Solution with deepcopy
deep_copied = copy.deepcopy(original)
deep_copied[1][0] = 77
print(original) # What happens now?
Options:
A) Both modify the original list
B)
copy()
creates fully independent copies C) Shallow copy affects nested objects, deepcopy doesn't
D)
deepcopy
is slower but creates true copies #Python #Interview #DeepCopy #MemoryManagement
✅ By: https://yangx.top/DataScienceQ
❤2