-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethods-DICTIONARIES.py
More file actions
179 lines (132 loc) · 3.89 KB
/
Methods-DICTIONARIES.py
File metadata and controls
179 lines (132 loc) · 3.89 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
########## METHODS DICTIONARIES ##########
## .clear()
# delete ALL items
polEngDict = {1:"imane",2:"ouweys",3:"soumaya"}
polEngDict.clear()
print(polEngDict)
# {}
## .copy()
# CopY a dictionary
myDict = {"A":1, "B":2}
copyDict = myDict.copy()
print(copyDict)
# {'A': 1, 'B': 2}
## .fromkeys()
x = ('key1', 'key2', 'key3')
y = ["un", "deux","trois"]
thisdict = dict.fromkeys(x, y)
print(thisdict)
# {'key1': ['un', 'deux', 'trois'], 'key2': ['un', 'deux', 'trois'], 'key3': ['un', 'deux', 'trois']}
x = ('key1', 'key2', 'key3')
y = ("un", "deux","trois")
thisdict = dict.fromkeys(x, y)
print(thisdict)
# {'key1': ['un', 'deux', 'trois'], 'key2': ['un', 'deux', 'trois'], 'key3': ['un', 'deux', 'trois']}
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)
# {'key1': 0, 'key2': 0, 'key3': 0}
# if we specify only one parameter, it will be used to define the "key" and associated value will be None
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x)
print(thisdict)
# {'key1': None, 'key2': None, 'key3': None}
## .get(value, default if non-existent)
picnicItems = {'apples': 5, 'cups': 2}
print ('I am bringing ' + str(picnicItems.get('cups', 0)) + ' cups.') # cups in dict and = 2
# I am bringing 2 cups.
print ('I am bringing ' + str(picnicItems.get('eggs', 100)) + ' eggs.') # eggs absent so by default = 100
# I am bringing 100 eggs.
## .items()
# item = pair key/value
spam = {'color': 'red', 'age': 42}
for i in spam.items():
print(i)
# ('color', 'red')
# ('age', 42)
## .keys()
spam = {'color': 'red', 'age': 42}
for k in spam.keys():
print(k)
# color
# age
dict = {"horse" : "cheval", "dog" : "chien", "cat" : "chat"}
for key in sorted(dict.keys()):
print(key, "->", dict[key])
# cat -> chat
# dog -> chien
# horse -> cheval
## .pop(value)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print (car)
# {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
car.pop("model")
print(car)
# {'brand': 'Ford', 'year': 1964}
## .popitem()
# WITHOUT parameter
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
dict.popitem() # delete the las item BUT BEFORE PYTHON 3.6.7, REMOVE one RANDOMLY !!!!
print(dict)
# {'cat' : 'chat', 'dog' : 'chien'}
# we can retrieve the "popped" item value
ui_elements = dict([('radio_button', 2),('text_box', 3),('standard_button', 5)])
popped_element = ui_elements.popitem()
print(list(popped_element))
# ['standard_button', 5]
## setdefault()
# If key exists, setdefault() return its value WITHOUT modify the dictionary, enven if VALUE = None
# If key does not exist, setdefault() ajouteadds key with default_value and returns default_value
car = {
"brand": "Ford",
"year": 1964
}
x = car.setdefault("model", "Bronco")
print(x)
# Bronco
print(car)
{'brand': 'Ford', 'year': 1964, 'model': 'Bronco'}
car = {
"brand": "Ford",
"model": None,
"year": 1964
}
x = car.setdefault("model", "Bronco")
print(x)
None
print(car)
{'brand': 'Ford', 'model': None, 'year': 1964}
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.setdefault("model", "Bronco")
print(x)
# Mustang
print(car)
# {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
## .update()
# update a dict with an other dict
polEngDict = {"kwiat" : "flower"}
add = {"gleba" : "soil"}
polEngDict.update(add)
print(polEngDict)
# {'kwiat': 'flower', 'gleba': 'soil'}
## .values()
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
for english, french in dict.values(): # value = 1 ==> does not work if we put an item
print(english, "->", french)
# ValueError: too many values to unpack (expected 2)
dict = {"cat" : "chat", "dog" : "chien", "horse" : "cheval"}
for french in dict.values():
print(french)
# chat
# chien
# cheval