Example 1: example of a deep copy in python
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list)
old_list[1][0] = 'BB'
print("Old list:", old_list)
print("New list:", new_list)
Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Example 2: python clone object
import copy
new_ob = copy.deepcopy(old_ob)
Example 3: python deepcopy
>>> import copy
>>> nums = [1, 2, 3]
>>> data = {'a': 10, 'b': nums}
>>> data
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy = copy.copy(data)
>>> data_deep = copy.deepcopy(data)
>>> data_copy
{'a': 10, 'b': [1, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy['a'] += 2
>>> nums[1:1] = [254]
>>> data
{'a': 10, 'b': [1, 254, 2, 3]}
>>> data_copy
{'a': 12, 'b': [1, 254, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
Example 4: .copy python
new_list = list.copy()
Example 5: python deep copy
x = [0,1]
y = x
x.append(2)
print(x)
print(y)
import copy
some_list = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
other_list = copy.copy(some_list)
some_list.append([3, 3, 3])
print(some_list)
print(other_list)
del some_list[2]
some_list[1][0] = 'One'
print(some_list)
print(other_list)