we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.
sometimes you may want to have the original values unchanged and only modify the new values or vice versa. In Python, there are two ways to create copies:
Shallow Copy
Deep Copy
Shallow Copy
A shallow copy creates a new object which stores the reference of the original elements.
import copy
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
print("Old list:", old_list)
print("New list:", new_list)
Deep Copy
A deep copy creates a new object and recursively adds the copies of nested objects present in the original elements
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list)
print("Old list:", old_list)
print("New list:", new_list)