-
Notifications
You must be signed in to change notification settings - Fork 0
/
6Dictionary.py
141 lines (120 loc) · 3.03 KB
/
6Dictionary.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Dictionary is used to store data in Key:Value format
# Dictionary is ordered and changeable, does not support duplicate value.
a_dic = {
'name': 'Anna',
'Age': 23,
'Location': 'UK',
'Height': 5.3,
'Girl': True
}
print(a_dic)
# -_-_-_-_-_-_-_-| Accessing Items in Dictionary |-_-_-_-_-_-_-_-
# values in dictionary can be accessed via Key
print(a_dic['name']) # this will print the value of 'name' key.
# _.get(key)
age = a_dic.get('Age') # This does the same
# _.keys()
# this will return a list of keys inside a dictionary
key_list = a_dic.keys()
# _.values()
# same as keys() but returns list of values.
values = a_dic.values()
# _.items()
# returns a list of the items in a dictionary
# where List contains individual tuples for individual items
# in that tuple the first item is key and second item is value
a_car = {
"car": 'BMW',
"price": 12000,
'Currency': 'USD',
'HP': 360,
'Color': 'Red'
}
itemList = a_car.items()
itemList = list(itemList) # by default items() returns 'dict_items' class object
# you have to convert it into a list
# -_-_-_-_-_-_-_-| Updating Dictionary |-_-_-_-_-_-_-_-
# _.update({key : value})
# this will update an existing key:value or add a new key:value
Amy = {
'age': 24,
'Home': 'USA',
'Salary': 1000,
'Year': 1990,
'Kids': 2
}
Amy.update({'age': 30}) # This will update the value of key 'age'
# Adding Items
Amy['weight'] = 65 # this will add a new item with key: 'weight' and value: 65
# Removing Items
# _.pop(key)
# this will remove a specified key from the Dictionary
Amy.pop('Kids')
# _.popitme()
# Removes item from dictionary Randomly
Amy.popitem()
# del
# del function has two uses
# 1> it can delete an specific item
# 2> it can delete the entire Dictionary
del Amy['Year'] # this will delete the item with key called 'year'
del Amy # this will delete the whole Dictionary
# _.clear()
# this clears the whole dictionary and returns an empty dictionary
number = {
'pi': 3.14,
'g': 9.8,
'escape_velocity': 11.2
}
number.clear()
# -_-_-_-_-_-_-_-| Copying Dictionary |-_-_-_-_-_-_-_-
words = {
'a': 'about',
'b': 'blank',
'c': 'cow',
'd': 'draw',
'e': 'egg',
'f': 'flower'
}
# _.copy()
copy_words = words.copy()
# dict()
copy_words2 = dict(words)
# -_-_-_-_-_-_-_-| Nested Dictionary |-_-_-_-_-_-_-_-
# you can put multiple dictionaries into a single dictionary. this is known as nesting
# The dictionaries inside the dictionary is called Nested Dictionaries
boys = {
'boy1': {
'name': 'Abe',
'age': 10
},
'boy2': {
'name': 'Ahmed',
'age': 12
},
'boy3': {
'name': 'Elon',
'age': 9
}
}
# or you can create multiple single dictionaries and put them into a mother dictionary
boy1 = {
'name': 'Josh',
'age': 12
}
#___________________________
boy2 = {
'name': 'Tom',
'age': 15
}
#___________________________
boy3 = {
'name': 'Karl',
'age': 16
}
#___________________________
all_boys = {
'Boy1': boy1,
'Boy2': boy2,
'Boy3': boy3
}