Xtralien Scientific Python: Dictionaries

 

Dictionaries are a core data construct in Python. They work by using a key to map to a value. This key can be any unique Python value and the value can also be any unique Python value. However, typically strings are used as keys due to their readability.

 Creating a dictionary

To create any dictionary you use {}, where the curly braces denote the beginning and end of the dictionary, like the square brackets for a list.

a = {}

The example above would create an empty dictionary. To populate a dictionary on creation you use a pattern like below.

a = {
    key: value,
    key: value
}

As you can see above, multiple entries are separated by a comma ,, with each entry consisting of a key and value.

A use case for a dictionary might be something like a small local database. In the example below a few periodic elements are mapped from their symbol to their name in English.

periodic_table = {
    'H': 'Hydrogen',
    'He': 'Helium',
    'Fe': 'Iron'
}

 Reading a dictionary 

To read from a dictionary you use a similar subscript style to that of lists. Continuing from the above example, we can get read the name of the element with H as it’s symbol. We do this by providing the key that we want to look up.

element_name = periodic_table['H']

The above example will load the value stored at key 'H' in the dictionary periodic_table. In this case that means that 'Hydrogen' is stored in the variable element_name.

In the case that the key doesn’t exist in the dictionary you will get a KeyError. This is similar to an IndexError that you get when you try to access a nonexistent list element. However, with dictionaries you can use the get method to return a default in the case that the key doesn’t exist.

element_name = periodic_table.get('H', "default value")

The above will store 'Hydrogen' in the variable element_name. We can also choose to search for a key that doesn’t exist, like 'Xe'.

element_name = periodic_table.get('Xe', "default value")

This will store the value "default value" in the variable element_name. This pattern of accessing databases is good for settings because you can set a default for any optional value.

Note: It is not possible to use slices with dictionaries

Updating a dictionary

If you wish to update a dictionary to contain a certain key-value then you can set the value directly.

periodic_table['Xe'] = "Xenon"

The above example will set the key 'Xe' in the dictionary periodic_table to the value "Xenon".

In cases where you want to set multiple values of a dictionary you have two options. You can either set each key separately as above, or you can update the dictionary using another dictionary.

periodic_table.update({
    'C': 'Carbon',
    'O': 'Oxygen'
})

If any of the keys already exist in the periodic_table dictionary then the new values are the ones stored, overwriting the old values. Any keys that don’t exist are created with the new values.

Deleting from a dictionary

To remove any elements from a dictionary you need to del the key from the dictionary. This will remove the key and the value from the dictionary.

del period_table['O']

The above example will remove the element with a key of 'O' from the dictionary. If the key doesn’t exist this will cause a KeyError, as if you were accessing the value at the key.