00:11 The purpose of a dictionary is to store data by mapping a key to a value. Some examples of this are mapping a name to a phone number or an IP address to a domain name. Given a key store like this, you could take the name from the data, look it up, and get the phone number as a result.
00:30 Same with the IP address and the domain name. Dictionary data structures are known by a bunch of different names in computer science. Python uses the native term dict for this kind of data structure, but other languages will refer to these as maps, hashmaps, lookup tables, and associative arrays.
01:01 It’s one of the most fundamental aspects of the language at the heart of its implementation. Python uses curly braces to indicate a dictionary. Here’s an example of a dictionary mapping people’s names to their phone extensions.
This dictionary contains three keys, the strings
"janet", and each key maps to a four-digit number, the extension. Inside of the REPL, if I type the name of the
phonebook, it gives me what’s called a repr, or the representation, of that.
01:41 It’s an automatic way of translating something to a string that shows you—typically, for debug purposes—the contents of the data structure. I can access any one of the keys by referencing it using square brackets and the name of the key.
If I try to ask for a key that’s not there, I’ll get an exception. The exception is a
KeyError, indicating that that key does not exist inside of the dictionary. In addition to using the square brackets to access a key, I can use the
.get() method on the dictionary.
This ends up with the same result, returning the
3719 we mapped to the key
.get() is subtly different from using the square brackets, in that it doesn’t actually trigger a
KeyError. If I call
This is a really handy way of getting something out of a dictionary and returning a default value if the key didn’t exist. This is far more succinct than using a try/catch block, catching the
KeyError, and then returning a value.
I can see all of the keys in the
phonebook using the
.keys() method on the dictionary. This returns a special generator called
dict_keys, which contains the values of
I can then print those out to the screen and get back all of the keys and values. In addition to explicitly defining the values like I did in the
phonebook, Python supports a mechanism called comprehensions.
A dictionary comprehension constructs the dictionary based on some calculation. The comprehension looks like a bit of a combination between the iterator part of a
for loop and the brace brackets themselves for a dictionary.
When trying to understand a comprehension, I like to start from the outsides of the sandwich. On the left side, you see
name:rank. That says you’re going to be creating some key-value terms—in this case, based on the ideas of the variables
rank. On the other end of the sandwich, on the right-hand side of the statement, is the
ranks list, which is defined above.
Once again, start on the left-hand side. This says it’s going to be creating a dictionary mapping
x to the value of
x * x, the square of
x. On the right-hand side, you can see that you’re going to be mapping values in the return from the function
range(6). That’s the values from
range() is not inclusive. The middle part of the comprehension says “Loop over those values.” The end result:
0 mapping to
25—the value and its corresponding square.
That way, when key-values are pushed into the dictionary and accessed using a different variable with the same contents, the result gets returned. Common hashable types built into Python are strings, numbers, and tuples of other hashable types. If you stick to these kinds of keys, you don’t have to worry about
.__eq__(), but if you’re going to build your own objects that you want to be hashable, then you’ll have to go off and implement those dunder methods.
As I mentioned earlier, the
dict type is a foundational part of Python, and it’s key to how Python actually implements a lot of concepts inside of the language. As a result, it’s highly optimized and written to be very performant.
08:41 It is O(1) (order 1) complexity for lookup, insert, update, and delete operations. That’s about as good as it gets. In addition to the built-in type, Python has other kinds of dictionaries as well.
Become a Member to join the conversation.