All of 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.
Sometimes, though, we need a more flexible way of representing data. Consider a store catalog with the names and prices of items for sale:
Table11.1.1.
Name
Price
Pencil
0.24
Notebook
3.95
Marker
0.79
We could use a nested list to represent this inventory.
Now, what happens if we get a user’s input with an item and a new price? Here’s the plan (remember, we make a plan before we start to write the code). We set up a while loop that goes through each entry in the catalog. If the first item in the sublist is the item we are looking for, we modify the second item in that sublist. If we get all the way through the catalog without finding the requested item, we can either give an error message or we can enter the data as a new entry. Here is the corresponding code:
Wow. That’s a lot of work. There must be a better way.
Python provides that better way in the form of a dictionary. A dictionary is collection that implements a mapping type. A map is an unordered collection where a key is associated with a value. The key can be any immutable type, and the value can be any object type.
In our catalog example, the keys are the item names and the prices are the values. Figure 11.1.2 shows what the storage map might look like. Instead of a strict left-to-right order, Python uses complex algorithms, designed for very fast access, to determine where the key-value pairs are stored in a dictionary.
One way to create a dictionary is to start with an empty dictionary and add key-value pairs. The empty dictionary is denoted {}
The first assignment creates an empty dictionary named catalog. The other assignments add new key-value pairs to the dictionary. The left hand side gives the dictionary and the key being associated. The right hand side gives 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.
Because the keys and values are stored in memory for efficiency, the order of the pairs that print produces may not be what you expected. For our purposes we can think of this ordering as unpredictable.
Another way to create a dictionary is to provide a list 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.
You put a key in the indexing operator [ ] to look up the corresponding value.
The key 'Notebook' yields the value 3.95.
Note11.1.3.
This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
Checkpoint11.1.4.
A dictionary is an unordered collection of key-value pairs.
False
Dictionaries associate keys with values but there is no assumed order for the entries.
True
Yes, dictionaries are associative collections, meaning that they store key-value pairs.