1
1
# Dictionaries
2
2
3
- ** TODO:** write the lists-and-tuples.md this tutorial links to.
4
-
5
3
Now we know how [ lists and tuples] ( lists-and-tuples.md ) work and how
6
- to [ for loop] ( loops.md#for-loops ) over them. We also did an exercise
7
- with code like this:
4
+ to [ for loop] ( loops.md#for-loops ) over them. If we make some kind of
5
+ program that needs to keep track of people's usernames and passwords,
6
+ we can use a list for that:
8
7
9
8
``` py
10
9
userlist = [
@@ -13,14 +12,14 @@ userlist = [
13
12
]
14
13
```
15
14
16
- Then to check if a username and password were correct we did
17
- ` (username, password) in userlist ` . Adding new users was also easy as
18
- appending to that list .
15
+ Then to check if a username and password are correct we can do
16
+ ` (username, password) in userlist ` . Or we can add a new user easily by
17
+ appending a new ` (username, password) ` tuple to the userlist .
19
18
20
- What if we need to check if a username is in the users , but we don't
21
- need to know the password? ` username in userlist ` is always False
22
- because the user list consists of (username,password) pairs, so we need
23
- to for loop over the whole list :
19
+ But what if we need to check if a username exists , but we don't know
20
+ the password? ` username in userlist ` is always False because the user
21
+ list consists of ` (username, password) ` pairs instead of just
22
+ usernames, so we need to for loop over the whole userlist :
24
23
25
24
``` py
26
25
username_exists = False
@@ -32,23 +31,24 @@ if username_exists:
32
31
# do something
33
32
```
34
33
35
- Getting a user's password also requires a similar loop:
34
+ Or how about getting a user's password if we know the username? This
35
+ also requires going through the whole list.
36
36
37
37
``` py
38
38
password = None
39
39
for user in userlist:
40
40
if user[0 ] == username:
41
41
password = user[1 ]
42
42
break
43
- # make sure password isn't still None and do something with it
43
+ # make sure password is not None and do something with it
44
44
```
45
45
46
- This works just fine because our user list only contains two users, but
47
- it would be slow if the userlist was bigger .
46
+ As you can see, a list of ` (username, password) ` pairs is not an ideal
47
+ way to store our usernames and passwords .
48
48
49
49
## What are dictionaries?
50
50
51
- A better way to store user information might be a dictionary.
51
+ A better way to store user information might be a dictionary:
52
52
53
53
``` py
54
54
passwords = {
@@ -64,8 +64,8 @@ values so I named the variable `passwords`.
64
64
65
65
There are a few big differences between dictionaries and lists of pairs:
66
66
67
- - Dictionaries are not ordered. There's ** no guarantees** about which
68
- order the username: password pairs appear in when we do something
67
+ - Dictionaries are not ordered. There are ** no guarantees** about which
68
+ order the ` username: password ` pairs appear in when we do something
69
69
with the dictionary.
70
70
- Checking if a key is in the dictionary is simple and fast. We don't
71
71
need to for loop through the whole dictionary.
@@ -93,7 +93,10 @@ variable names and values are what our variables point to.
93
93
```
94
94
95
95
So if you have trouble remembering how dictionaries work just compare
96
- them to variables.
96
+ them to variables. A dictionary is a perfect way to store our usernames
97
+ and passwords. We don't care about which order the users were added in,
98
+ it's impossible to add multiple users with the same username and
99
+ getting a user's password is easy.
97
100
98
101
## What can we do with dictionaries?
99
102
@@ -108,28 +111,36 @@ lists and dictionaries have a length.
108
111
>> >
109
112
```
110
113
111
- We can get a value of a key with ` the_dict[key] ` . Trying to get the
112
- value of a non-existing key gives us an error. We can also add new
113
- key: value pairs by doing ` the_dict[key] = value ` .
114
+ We can get a value of a key with ` the_dict[key] ` . This is a lot easier
115
+ and faster than for-looping over a list of pairs.
114
116
115
117
``` py
116
118
>> > passwords[' me' ]
117
119
' my password'
118
120
>> > passwords[' you' ]
119
121
' your password'
122
+ >> >
123
+ ```
124
+
125
+ Trying to get the value of a non-existing key gives us an error, but we
126
+ can add new ` key: value ` pairs by doing ` the_dict[key] = value ` .
127
+
128
+ ``` py
120
129
>> > passwords[' lol' ]
121
130
Traceback (most recent call last):
122
131
File " <stdin>" , line 1 , in < module>
123
132
KeyError : ' lol'
124
133
>> > passwords[" lol" ] = " lol's password"
134
+ >> > passwords[" lol" ]
135
+ " lol's password"
125
136
>> > passwords
126
137
{' lol' : " lol's password" , ' you' : ' your password' , ' me' : ' my password' }
127
138
>> >
128
139
```
129
140
130
- For looping over a dictionary gets its keys, and checking if something's
131
- in the dictionary checks if the dictionary has a key like that. This can
132
- be confusing at first but you'll get used to this.
141
+ For looping over a dictionary gets its keys, and checking if something
142
+ is in the dictionary checks if the dictionary has a key like that. This
143
+ can be confusing at first but you'll get used to this.
133
144
134
145
``` py
135
146
>> > ' me' in passwords
@@ -190,7 +201,101 @@ me: my password
190
201
>> >
191
202
```
192
203
193
- ** TODO:** lists as keys vs tuples as keys.
204
+ This is also useful for checking if the dictionary has a ` key: value `
205
+ pair.
206
+
207
+ ``` py
208
+ >> > (' me' , ' my password' ) in passwords.items() # correct username and password
209
+ True
210
+ >> > (' me' , ' whatever' ) in passwords.items() # wrong username or password
211
+ False
212
+ >> >
213
+ ```
214
+
215
+ ## Limitations
216
+
217
+ Sometimes it might be handy to use lists as dictionary keys, but it
218
+ just doesn't work. I'm not going to explain why Python doesn't allow
219
+ this because usually we don't need to worry about that.
220
+
221
+ ``` py
222
+ >> > stuff = {[' a' , ' b' ]: ' c' , [' d' , ' e' ]: ' f' }
223
+ Traceback (most recent call last):
224
+ File " <stdin>" , line 1 , in < module>
225
+ TypeError : unhashable type : ' list'
226
+ >> >
227
+ ```
228
+
229
+ On the other hand, tuples work just fine:
230
+
231
+ ``` py
232
+ >> > stuff = {(' a' , ' b' ): ' c' , (' d' , ' e' ): ' f' }
233
+ >> > stuff
234
+ {(' a' , ' b' ): ' c' , (' d' , ' e' ): ' f' }
235
+ >> >
236
+ ```
237
+
238
+ The values of a dictionary can be anything.
239
+
240
+ ``` py
241
+ >> > stuff = {' a' : [1 , 2 , 3 ], ' b' : [4 , 5 , 6 ]}
242
+ >> > stuff
243
+ {' a' : [1 , 2 , 3 ], ' b' : [4 , 5 , 6 ]}
244
+ >> >
245
+ ```
246
+
247
+ ## Summary
248
+
249
+ - Dictionaries consist of ` key: value ` pairs.
250
+ - Variables are stored in a dictionary with their names as keys, so
251
+ dictionaries behave a lot like variables:
252
+ - Dictionaries are not ordered.
253
+ - Setting or getting the value of a key is simple and fast.
254
+ - Dictionaries can't contain the same key more than once.
255
+ - For-looping over a dictionary loops over its keys, and checking if
256
+ something is in the dictionary checks if the dictionary has a key
257
+ like that. The ` values() ` and ` items() ` methods return things that
258
+ behave like lists of values or ` (key, value) ` pairs instead.
259
+
260
+ ## Examples
261
+
262
+ This program counts how many times words appear in a sentence.
263
+ ` sentence.split() ` creates a list of words in the sentence, see
264
+ ` help(str.split) ` for more info.
265
+
266
+ ``` py
267
+ sentence = input (" Enter a sentence: " )
268
+
269
+ counts = {} # {word: count, ...}
270
+ for word in sentence.split():
271
+ if word in counts:
272
+ # we have seen this word before
273
+ counts[word] += 1
274
+ else :
275
+ # this is the first time this word occurs
276
+ counts[word] = 1
277
+
278
+ print () # display an empty line
279
+ for word, count in counts.items():
280
+ if count == 1 :
281
+ # "1 times" looks weird
282
+ print (word, " appears once in the sentence" )
283
+ else :
284
+ print (word, " appears" , count, " times in the sentence" )
285
+ ```
286
+
287
+ Running the program might look like this:
288
+
289
+ Enter a sentence: this is a test and this is quite long because this is a test
290
+
291
+ is appears 3 times in the sentence
292
+ long appears once in the sentence
293
+ a appears 2 times in the sentence
294
+ because appears once in the sentence
295
+ this appears 3 times in the sentence
296
+ quite appears once in the sentence
297
+ and appears once in the sentence
298
+ test appears 2 times in the sentence
194
299
195
300
***
196
301
0 commit comments