Reviewing Dictionaries
00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll give a quick review of dictionaries and how you construct them without a comprehension.
00:09
A dictionary is a data structure that maps values to keys. This kind of structure is also known as a hash, hash table, hash-map, or associative array. Python has a dictionary built in, and it’s called the dict
.
00:24 Let’s say you had a set of things to sell in a store. You could have the things for sale be the keys and the prices be the values. Then you could look up the pen key to find out that it’s $865.
00:37 It’s a really nice pen. Dictionaries are optimized for retrieval. If I had a list of items in the store, trying to find one means searching through the whole list.
00:48
Well, with a dict
, I’m looking things up by key. The length of time to look something up for a dict
is more or less independent of the number of items in the dict
.
00:58 I say more or less because there are some edge cases, but for our purposes, let’s stick with their independent. The reason a dictionary is sometimes called a hash or hash table, is because it’s based on a hash function.
01:11 A hash function is any function that maps a piece of data to a slot. The slot, in this case, being where you’re going to store the associated value. There’s lots of math behind writing a good hash function and dealing with the corner cases called hash collisions where two different items map to the same place, but you really don’t need any of those details to play with dictionaries.
01:32 Not all things are hashable, and for an object to be used as a key in Python, it has to work with Python’s hash function. Most common data types are hashable though, so things like strings and numbers are fine.
01:44 You’re not likely to run into a problem unless you’re doing something funky. Let’s head into the REPL and review how you construct a dictionary. You use brace brackets to define a dictionary.
01:58 Here I’m going to construct a mapping between fruit and their prices.
02:05
Each key-value pair is defined with a colon. The string apple
is my key with the float 40 cents as the value. I can add more than one key-value pair as part of my dictionary definition by using a comma like I have here.
02:22
orange
is my second piece of fruit, pear
is my third, and then I finish the dictionary with a closing brace. Let’s look at what I just defined, and you can see the key-value pairs.
02:37 A quick aside: In the real world, you should never use floats for prices as funky things can happen as decimals in floating point may not behave the way you expect.
02:46
Instead, you should use the capital D Decimal
class, but for the sake of brevity, I’m sticking with floats here. You retrieve a value from a dictionary by using square brackets to reference the key.
03:00 You can also use the same mechanism to modify a value.
03:05 My apple is now 45 cents. You can also use this mechanism to add new key-value pairs to the dictionary.
03:17 Yes, sir. We have some bananas today. For clarity, when I wrote the fruit price dictionary, I split it up on multiple lines, but you don’t have to.
03:32
Here’s the result. Everything in Python is an object, and the brace mechanism is just a short form for creating a dict
object. You can also create one like any other class.
03:47
Don’t be fooled by the lowercase here. dict
is a class, and this is instantiating an object using the arguments to the constructor as the key-value pairs in the result.
03:59
This dictionary shows you that you have a choice. You could buy a Kia or 48 and a half pens. So far, I’ve been creating a dict
all at once, but frequently you are going to be adding and changing values as you go along.
04:12
One common pattern is to use a loop to calculate values and store them in a dict
.
04:20
This dict
is going to store the first nine powers of two.
04:27
My loop ranges from one to nine. range(
10)
is exclusive at the end,
04:36
and for each number, I’m storing a key-value pair of the number and two to the power of that number. Let’s look at the result, and there you go. You’ve calculated values and stored them in a dict
.
04:53 In the next lesson, I’ll show you how to do that same thing that you just did in a loop, but with a single line of code.
Become a Member to join the conversation.