forked from abhi1540/PythonConceptExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepcopy_shallowcopy.py
More file actions
60 lines (42 loc) · 1.29 KB
/
deepcopy_shallowcopy.py
File metadata and controls
60 lines (42 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Python code to demonstrate copy operations
# importing "copy" for copy operations
import copy
#############################DEEP COPY################################
# initializing list 1
li1 = [1, 2, [3, 5], 4]
# using deepcopy to deep copy
li2 = copy.deepcopy(li1)
# original elements of list
print("The original elements before deep copying")
for i in range(0, len(li1)):
print(li1[i], end=" ")
print("\r")
# adding and element to new list
li2[2][0] = 7
# Change is reflected in l2
print("The new list of elements after deep copying ")
for i in range(0, len(li1)):
print(li2[i], end=" ")
print("\r")
# Change is NOT reflected in original list
# as it is a deep copy
print("The original elements after deep copying")
for i in range(0, len(li1)):
print(li1[i], end=" ")
#############################SHALLOW COPY#########################
import copy
# initializing list 1
li1 = [1, 2, [3, 5], 4]
# using copy to shallow copy
li2 = copy.copy(li1)
# original elements of list
print("The original elements before shallow copying")
for i in range(0, len(li1)):
print(li1[i], end=" ")
print("\r")
# adding and element to new list
li2[2][0] = 7
# checking if change is reflected
print("The original elements after shallow copying")
for i in range(0, len(li1)):
print(li1[i], end=" ")