-
Notifications
You must be signed in to change notification settings - Fork 784
/
pythonDict
93 lines (75 loc) · 3 KB
/
pythonDict
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#fromkeys(key,val): used to take keys and assign values from iterable
#creaing dict from list using fromkeys() method
list1 = [1,2,3,4,5]
my_dict = dict.fromkeys(list1)
print("dict", my_dict)
#creating list using range()
print({}.fromkeys(range(1,7),10))
#--------------------------------------------------------------------
#setdefault(key, values): using this method we can check if the value is present in the dictionary, if not it will add the value
my_dict.setdefault(6)
print(my_dict)
#--------------------------------------------------------------------
#update()
#concatenating 2 dict
d1 = {1:'one',2:'two',3:'three'}
d2 = {4:'four', 5:'five', 6:'six'}
print(d1.update(d2))
print(d1)
#adding list values in dict------------------------------------------
list1 = [7,'seven']
d1.update([list1])
print(d1)
#adding expressions using update command
print(d1)
#-----------------------------------------------------------------
#pop()-return value of the element to be deleted
dict_abc = {'a':'cat', 'b':'bat', 'c':'dog'}
print("dictionary before deleting ele: ",dict_abc)
#del---------------------------------------------------------------
del dict_abc['a']
#pop()-------------------------------------------------------------
dict_abc.pop('b')
print("after deleting element: ",dict_abc)
#clear()-used to delete all elements from a dictionary--------------
dict_abc.clear()
print("after deleting all elements from a dictionary", dict_abc)
# to delete the actual dictionary del keyword+dict_name is used------m
del dict_abc#fromkeys(key,val): used to take keys and assign values from iterable
#creaing dict from list using fromkeys() method
list1 = [1,2,3,4,5]
my_dict = dict.fromkeys(list1)
print("dict", my_dict)
#creating list using range()
print({}.fromkeys(range(1,7),10))
#--------------------------------------------------------------------
#setdefault(key, values): using this method we can check if the value is present in the dictionary, if not it will add the value
my_dict.setdefault(6)
print(my_dict)
#--------------------------------------------------------------------
#update()
#concatenating 2 dict
d1 = {1:'one',2:'two',3:'three'}
d2 = {4:'four', 5:'five', 6:'six'}
print(d1.update(d2))
print(d1)
#adding list values in dict------------------------------------------
list1 = [7,'seven']
d1.update([list1])
print(d1)
#adding expressions using update command
print(d1)
#-----------------------------------------------------------------
#pop()-return value of the element to be deleted
dict_abc = {'a':'cat', 'b':'bat', 'c':'dog'}
print("dictionary before deleting ele: ",dict_abc)
#del---------------------------------------------------------------
del dict_abc['a']
#pop()-------------------------------------------------------------
dict_abc.pop('b')
print("after deleting element: ",dict_abc)
#clear()-used to delete all elements from a dictionary--------------
dict_abc.clear()
print("after deleting all elements from a dictionary", dict_abc)
# to delete the actual dictionary del keyword+dict_name is used------m
del dict_abc