-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path041.CopyInDictionary.py
More file actions
39 lines (32 loc) · 934 Bytes
/
041.CopyInDictionary.py
File metadata and controls
39 lines (32 loc) · 934 Bytes
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
#this import copy module which can be used to create deep copy
import copy
#.copy() function only creates a shallow copy
lion_list = ["scary","big","cat"]
elephant_list = ["big","grey","wrinkled"]
teddy_list = ["cuddly","stuffed"]
animals = {
"lion" : lion_list,
"elephant" : elephant_list,
"teddy" : teddy_list,
}
print(".copy() or shallow copy:-")
#Shallow Copy:-
things = animals.copy()
print(id(things["teddy"]),things["teddy"])
print(id(animals["teddy"]),animals["teddy"])
print()
teddy_list.append("toy")
print(id(things["teddy"]),things["teddy"])
print(id(animals["teddy"]),animals["teddy"])
print()
print()
print("copy.deepcopy() or deep copy:-")
#deep copy
things = copy.deepcopy(animals)
print(id(things["teddy"]),things["teddy"])
print(id(animals["teddy"]),animals["teddy"])
print()
teddy_list.append("toy")
print(id(things["teddy"]),things["teddy"])
print(id(animals["teddy"]),animals["teddy"])
print()