ubuntu2004
Dictionaries I - the basics
Dictionaries comprise items of key:value pairs
As we've seen, a list variable allows us to store many values simultaneously in a single variable, for example
stores six strings in a single list variable called cell_types
.
There is another variable type in Python that allows us to store values simultaneously in a single variable: a dictionary.
Dictionaries are a little more complicated than lists though. The best way to think of them is a table with just two columns and as many rows as is needed. Let's revisit the table of average cell diameters of human white blood cells:
Cell type | Diameter (m) |
---|---|
Neutrophil | 11 |
Eosinophil | 11 |
Basophil | 13.5 |
Small lymphocyte | 7.5 |
Large lymphocyte | 13.5 |
Monocyte | 22.5 |
This table can be represented as a Python dictionary and assigned to a dictionary variable. Let's call it white_cells
:
The first thing to notice are the curly brackets { }
. Remember, lists have square brackets []
and dictionaries have curly brackets { }
.
Each row in the above table is represented in the dictionary. The notation is important. A colon separates each pair of values. And every pair of values is separated by a comma.
Note that we don't need to write each pair on a separate line. This was done for clarity and to show the correspondence between the table and the dictionary. It's perfectly acceptable and normal to write the dictionary on a single line like so:
Nor do we need spaces either side of the colon. You can choose whichever format you prefer.
Now for some jargon. The cell types (Neutrophil, Eosinophil, etc.) are called dictionary keys (or keys for short), and their diameters (11, 11, 13.5, etc) are called dictionary values (or values for short). So each item in a dictionary (corresponding to a row in a table) is made up of a key : value pair.
Note we will use the terms "item" and "key:value pair" interchangeably.
Dictionary keys are unique
There is an important restriction in dictionaries: each key must be unique. In other words, we are not allowed to have the same key appearing more than once in a dictionary. For example we are not allowed to have Neutrophil
appearing twice.
This restriction is actually a good thing and makes dictionaries very useful for many tasks.
Run the following code in which the key Neutrophil
appears twice but with different values of 11 and 12.
Notice that the dictionary only contains the key:value pair of the last instance of the key Neutrophil
. The first instance of Neutrophil
is ignored.
Dictionary values do not need to be unique
Unlike dictionary keys, dictionary values do not need to be unique. Notice in the dictionary white_cells
that Neutrophil
and Eosinophil
both have the same diameter value of 11.
Getting the number of items
The number of items in a dictionary is called its length. And, as for strings and lists, we use the function len()
to get a dictionary's length.
Getting the value of a key
A very common thing to do with dictionaries is to get the value of a key:value pair by supplying the key.
For example if you want the diameter of basophil cells you would use the code in the following cell. The value can be assigned to a variable if needed.
Note the notation here. The key Basophil
is surrounded by square brackets and immediately follows the name of the dictionary. This does not mean that ['Basophil']
is a list as you might expect. When working with a dictionary the square brackets refer to a key in that dictionary.
KeyError
If we use a key that isn't in the dictionary we get a KeyError
.
Try running the following code where we use the key Macrophage
which isn't in our dictionary.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-5-4513c9835af2> in <module>
1 # Printing the value of a key not in the dictionary gives a KeyError
----> 2 print( white_cells['Macrophage'] )
KeyError: 'Macrophage'
Adding an item
Say we had created our dictionary of white cell diameters but forgotten to include monocytes. We can add this item easily to the dictionary.
Changing the value of an item
The following code demonstrates how to change the value of an item.
Creating an empty dictionary
Sometimes we want to start with an empty dictionary and add items as we go along. We'll see an example of this in a later Notebook.
The following code shows how to initialise an empty dictionary.
Testing if a key is already in a dictionary
We might want to test if a key already exists in a dictionary. Depending on the answer you might want to take different actions.
To test if a key already exists in a dictionary we use the in
membership operator as we did for strings and lists.
Run the following code to see how this works.
We assign the string 'Leucocyte' to the string variable cell_type
and then test if 'Leucocyte' is a key in the dictionary white_cells
. It isn't so we output that a leucocyte is not a white blood cell.
Change Leucocyte
to Neutrophil
and rerun the code. The output will be that a neutrophil is a white blood cell.