-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictRevision.py
107 lines (59 loc) · 2.19 KB
/
dictRevision.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
# dictionary are key value pairs
# since version 3.7 python dictionaries are ordered
# keys are immutable that is once defined you can not change them
# keys are case sensitive
# more readable this way
phone_number = {
"Munya": 1234,
"Vaskr": 6789,
"Skidy": 101112
}
# another way to define a dictionary
# phone_number = dict({
# "Munya": 1234,
# "Vaskr": 6789,
# "Skidy": 101112
# })
# phone_number = dict([("Munya":1234),("Radzi":1234),("Muti":1234)])
phone_number["Munya"] = {"work":9999,"office":77777}
phone_number["peater"] = {5656565,2323,0o245,8989}
# one way to access the values in the dictionary using get method
# print(phone_number.get("vaskr")) # get method is safe to use incase there is a typo or keys is not present
# print(phone_number["Skidy"])
print("*******************************************************************")
# Home Work
# data = {
# 1:'Jenny',
# 2:'Ram',
# 0:'Mohan'
# }
# print(data.get(0)) #output is Mohan
print(phone_number)
# use the del keyword to remove keys and values from a dictionary
del phone_number["peater"]
print(phone_number)
# pop removes the specified item and return the corresponding value
print(phone_number.pop('Skidy'))
print(phone_number)
print(phone_number.popitem()) #removes the last item and returns its key and value
print(phone_number)
# if you want to access only keys in the dictonary then use the key() method
print(phone_number.keys())
# likewise if you want to access only the values of the dictionary there is the values() method
print(phone_number.values())
# items() method will create tuple from the dictionary
print(phone_number.items())
print(dict(phone_number.items()))
for i in phone_number:
for j in phone_number[i]:
print(j,phone_number[i][j])
print("############################################################################")
for i in phone_number.items():
print(i)
# copying one dictionary to another dictionary
phone_num2 = phone_number.copy()
print(phone_num2)
# getting the length of a dictionary
print(len(phone_num2))
phone_number.clear() #this will clear the whole dictionary
print(phone_number)