Python Dictionaries

Dictionaries in Python store data in key and value pairs. Unlike lists, where items are accessible using their numeric index value, items in a dictionary are accessible via their key, which can either be a string, number, boolean or tuple. Due to the absence of an index, dictionaries have no order.

The values stored in a dictionary can be any Python variable type, including another dictionary. Where more than one key and value pair needs to be stored in a dictionary, a comma needs to be used to separate each pair. Below is an example of a dictionary, called ‘person’, which has two key and value pairs, where the keys are ‘name’ and ‘age’.

person = {'name': 'George', 'age': '30'}

It is possible to display a value from a dictionary using the dictionary name and the key for the value that is required.


The resulting output from the above will be displayed in the console as follows.


Displaying the values in this manner can also be accomplished with a ‘for’ loop. The advantage of this method is that if a key and value pair is added to the dictionary, this code won’t need to change. The only drawback is that there are no guarantees as to the order of the values being displayed. In this case the ’30’ could appear above ‘George’.

for key in person:

As well as being able to display the values on their own, it is also possible to use them in the formulation of a string.

age_string = "{} is {} years old."
print(age_string.format(person['name'], person['age']))

The string ‘age_string’ will be displayed with the dictionary values incorporated into it in place of the placeholders ‘{}’.

George is 30 years old.

Instead of placeholders being represented by empty curly braces, a name can be given to them, which simplifies the formatting of a string. Note that the placeholder names must correspond with the keys in the dictionary for this to work. The following example outputs the same string shown above.

age_string = "{name} is {age} years old."

Dictionaries share the flexibility of lists with the ease of adding and removing key and value pairs. Here a key of ‘city’, with a value of ‘London’ is added to the ‘person’ dictionary. Note that if the key ‘city’ already existed, then this would just update its value.

person['city'] = 'London'

The dictionary could be returned to its original state by removing the key and value pair, ‘city’ and ‘London’ with the ‘del’ function.

del person['city']

As mentioned previously, the values in a dictionary can be another dictionary. The above example could be re-written to incorporate both a person’s first and last names.

person = {'name': {'last': 'Smith', 'first': 'George'}, 'age': "30"}
age_string = "{} {} is {} years old."
print(age_string.format(person['name']['first'], person['name']['last'], person['age']))

The string displayed in the console now looks like this.

George Smith is 30 years old.

If it were necessary to store and display details of multiple people, then multiple dictionaries could be used in conjunction with a list, to create a list of dictionaries. A ‘for’ loop could be used to cycle through the list, displaying the values from each of the dictionaries.

people = [
   {'name': {'last': 'Smith', 'first': 'George'}, 'age': "30"},
   {'name': {'last': 'Jones', 'first': 'Bob'}, 'age': "54"},
   {'name': {'last': 'Andrews', 'first': 'Fred'}, 'age': "21"}

age_string = "{} {} is {} years old."

for person in people:
   print(age_string.format(person['name']['first'], person['name']['last'], person['age']))

The console will display a string for each dictionary in the list.

George Smith is 30 years old.
Bob Jones is 54 years old.
Fred Andrews is 21 years old.