-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdictionary_methods.py
More file actions
59 lines (47 loc) · 1.38 KB
/
dictionary_methods.py
File metadata and controls
59 lines (47 loc) · 1.38 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
#METHODS OF DICTIONARY
#clear()-Removes all the elements from dictionary
l1={"k1":"1","k2":"2","k3":"3"}
l1.clear()
print(l1)
#copy()-Returns a copy of the dictionary
l1={"k1":"1","k2":"2","k3":"3"}
i=l1.copy()
print(i)
#fromkeys()-Returns a dictionary with a specified keys and value
#creates a dictionary with 3
i=('k1','k2','k3')
j=7
l1=dict.fromkeys(i,j)
print(l1)
#get()-Returns the value of the specified key
l1={"k1":"1","k2":"2","k3":"3"}
i=l1.get("k2")
print(i)
#items()-Returns a list containing tuple for each key-value pairs
l1={"k1":"1","k2":"2","k3":"3"}
i=l1.items()
print(i)
#keys()-Returns a list containing dictionary's keys
l1={"k1":"1","k2":"2","k3":"3"}
i=l1.keys()
print(i)
#pop()-Removes the element with a specified key
l1={"k1":"1","k2":"2","k3":"3"}
l1.pop("k2")
print(l1)
#popitem()-Returns the last inserted key-value pair
l1={"k1":"1","k2":"2","k3":"3"}
l1.popitem()
print(l1)
#setdefault()-Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
l1={"k1":"1","k2":"2","k3":"3"}
l1.setdefault("k4", "SHIVAM")
print(l1)
#update()-Updates the dictionary with the specified key-value pairs
l1={"k1":"1","k2":"2","k3":"3"}
l1.update({"k5":"tree"})
print(l1)
#values()-Returns a list of all values in the dictionary
l1={"k1":"1","k2":"2","k3":"3"}
i=l1.values()
print(i)