1# deep copy example in python
2import copy
3
4old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
5new_list = copy.deepcopy(old_list)
6
7old_list[1][0] = 'BB'
8
9print("Old list:", old_list)
10print("New list:", new_list)
11
12# OUTPUT
13Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]]
14New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
1>>> import copy
2>>> nums = [1, 2, 3]
3>>> data = {'a': 10, 'b': nums}
4>>> data
5{'a': 10, 'b': [1, 2, 3]}
6>>> data_copy = copy.copy(data)
7>>> data_deep = copy.deepcopy(data)
8>>> data_copy
9{'a': 10, 'b': [1, 2, 3]}
10>>> data_deep
11{'a': 10, 'b': [1, 2, 3]}
12>>> data_copy['a'] += 2
13>>> nums[1:1] = [254]
14>>> data
15{'a': 10, 'b': [1, 254, 2, 3]}
16>>> data_copy
17{'a': 12, 'b': [1, 254, 2, 3]}
18>>> data_deep
19{'a': 10, 'b': [1, 2, 3]}