Source Code:
(back to article)
# Shallow copy example original_list = [[1, 2], [3, 4]] shallow_copy = original_list.copy() # Modify the original list original_list[0][0] = 0 # The shallow copy reflects the changes made to the original list print(shallow_copy) # Output: [[0, 2], [3, 4]] # Deep copy example import copy original_list = [[1, 2], [3, 4]] deep_copy = copy.deepcopy(original_list) # Modify the original list original_list[0][0] = 0 # The deep copy does not reflect the changes made to the original list print(deep_copy) # Output: [[1, 2], [3, 4]]
Result:
Report an issue