1import copy
2
3l1 = [1,3,[9,4],6,8]
4l2 = copy.copy(l1) #Making a shallow copy
5
6print('List 1 = ', l1)
7print('List 2 = ', l2)
8
9print('Performing change in list 2')
10l2[2][0] = 5
11
12print('List 1 = ',l1)
13print('List 2 = ',l2)
14
1import copy
2
3l1=[1,3,[9,4],6,8]
4l2=copy.deepcopy(l1) #Making a deep copy
5
6print('List 1 = ', l1)
7print('List 2 = ', l2)
8
9print('Performing change in list 2')
10l2[2][0] = 5
11
12print('List 1 = ',l1)
13print('List 2 = ',l2)
1# explaining why we need deepcopy
2x = [0,1]
3y = x
4x.append(2)
5print(x)
6print(y)
7# result: [0, 1, 2]
8# result: [0, 1, 2]
9
10import copy
11
12some_list = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
13other_list = copy.copy(some_list)
14some_list.append([3, 3, 3])
15print(some_list)
16print(other_list)
17# result: [0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]]
18# result: [0, 0, 0], [1, 1, 1], [2, 2, 2]]
19
20del some_list[2]
21some_list[1][0] = 'One'
22print(some_list)
23print(other_list)
24# result: [0, 0, 0], ['One', 1, 1], [2, 2, 2]]
25# result: [0, 0, 0], ['One', 1, 1], [2, 2, 2]]
26
27#this problem doesn't happen with copy.deepcopy()
28
29