What is a dictionary?

The compound data types we have studied in detail so far — strings and lists — are sequential collections. This means that the items in the collection are ordered from left to right, and they use integers as indices to access the values they contain (0, 1, 2, 3, 4 …). This also means that looking for a particular value requires scanning the many items in the list until you find the desired value. Say that you wanted to find the word “cool” in a list, then you would have to check every single item in that list and find out whether it is the word you’re looking for. If we had a list of a billion words, this would take a lot of time!

Data can sometimes be organized more usefully by associating a key with the value we are looking for. For example, if you are asked for the page number for the start of chapter 5 in a large textbook, you might flip around the book looking for the chapter 5 heading. If the chapter number appears in the header or footer of each page, you might be able to find the page number fairly quickly, but it’s generally easier and faster to go to the index page and see that chapter 5 starts on page 78.

Using a list for some tasks would be akin to looking at every page of the book and saying to yourself, “does chapter 5 start on this page?” every single time you flip the page. Not very efficient. Sometimes using a dictionary, which tells you exactly where chapter 5 is, would be much quicker.

This sort of direct look-up of a value in Python is done with an object called a Dictionary. Dictionaries are a different kind of collection. They are Python’s built-in mapping type. A map is an unordered, associative collection. The association, or mapping, is from a key, which can be of any immutable type (e.g., the chapter name and number in the analogy above), to a value (the starting page number), which can be any Python data object. The key is associated with the value.

In this topic we’ll take a look at how to create dictionaries, and how to use them.

Getting Started

Let us look at an example of using a dictionary for a simple problem. We will create a dictionary to translate English words into Spanish. For this dictionary, the keys are strings, and the values will also be strings.

One way to create a dictionary is to start with an empty dictionary and add key-value pairs. The empty dictionary is denoted by {}.

The first assignment creates an empty dictionary named eng2sp. The other assignments add new key-value pairs to the dictionary. The left-hand side shows the dictionary and the key being associated. The right-hand side shows the value being associated with that key. We can print the current value of the dictionary in the usual way. The key-value pairs of the dictionary are separated by commas. Each pair contains a key and a value separated by a colon.

The order of the pairs may not be what you expected. Python uses complex algorithms, designed for very fast access, to determine where the key-value pairs are stored in a dictionary. For our purposes we can think of this ordering as unpredictable * .

Another way to create a dictionary is to provide a bunch of key-value pairs using the same syntax as the previous output.

It doesn’t matter what order we write the pairs. The values in a dictionary are accessed with keys, not with indices, so there is no need to care about ordering.

Here is how we use a key to look up the corresponding value.