إجابة مرجعية
While the = operator will copy many things in Python, it will not copy a Python object. It only creates a reference to the object. To create a copy of an object in Python, you need to use the copy module. The copy module offers two ways of copying an object.
• Shallow copy: Copies an object and re-use references from the old object
• Deep copy: Copies all the values in an object recursively.
from copy import copy, deepcopy
list_1 = [1, 2, [3, 5], 4]
## shallow
list_2 = copy(list_1)
list_2[3] = 11
list_2[2].append(12)
list_2 # output => [1, 2, [3, 5, 12], 11]
list_1 # output => [1, 2, [3, 5, 12], 4]
## deep
list_3 = deepcopy(list_1)
list_3[3] = 10
list_3[2].append(13)
list_3 # output => [1, 2, [3, 5, 6, 13], 10]
list_1 # output => [1, 2, [3, 5, 6], 4]