Skip main navigation

New lower prices! Get up to 50% off 1000s of courses. 

Explore courses

Structuring Data in Your Programs Using Dictionaries

Learn more about structuring data in your programs using dictionaries.

You may think of a dictionary in programming as being like a reference book used to understand the meaning of words. If you want to find the meaning of a word, then you find the word in the dictionary, and then read the meaning.

In the programming world, a dictionary is a data structure that is used to store data using a key and value system, just like a paper dictionary, with the word you are looking for being the ‘key’ and the definition is the ‘value’. Inside a Python dictionary, I can store any type of object, and when presented with a key the dictionary will return the value associated with that key. To create a Python dictionary, the entries are written with each key and corresponding value separated by a colon. Each key/value pair in the dictionary is separated by a comma:

Diagram to show how a dictionary works

In this step, you’ll see how to read from, write to, and remove data from a dictionary so that you can use this structure in your projects.

Storing Data in a Dictionary

Here I have created a dictionary called foods and am using the dictionary to store the favourite food of three people. In your Python editor create this same dictionary.

 

foods = {"Sarah":"Cheese", "Dexter":"Chicken", "Li":"Burgers"}

 

The name of each person is the key, and if I use that key the dictionary will tell me the value associated with that key, in this case, their favourite food. To see all of the persons (keys) and their favourite food (value) I can write code to print the entire dictionary.

 

Reading an Individual Value

 

There will be times when I want to see just a single person’s favourite food. How can I find out Sarah’s favourite food? Add this line to your code and then click Run.

 

print(foods["Sarah"])

 

To retrieve Sarah’s favourite food you need to get the value which is stored for the key "Sarah" in the foods dictionary. When this code is run you will see that Sarah’s favourite food is "Cheese".

 

Can you print Dexter’s and Li’s favourite foods to the screen?

 

Adding a New Person

 

To add another person and their favourite food to the foods dictionary you can assign a new value to a new key. You do this by using the name of the dictionary foods and the key (in this case, the name of the person), in square brackets, and then assigning the value (their favourite food) to it. Add these lines to your code:

 

foods["Amy"] = "Ice cream"
print(foods)

 

This code will add "Amy" and her favourite food, "Ice cream", to the dictionary. The second line of code will print the entire dictionary to the REPL, so that you can see that Amy is there.

 

Tip: When you use a dictionary you always retrieve values by key, never by position (index).

 

Updating a Person

 

If Sarah changed her mind, how could you update Sarah’s favourite food? Add these lines to your code.

 

foods.update({"Sarah":"Steak"})
print(foods)

 

This code updates a single value in the foods dictionary using the update method. This method requires the key, in this case the name "Sarah", to identify which value to change. It also requires the new value, in this case "Steak", which will replace the previously stored value. The final line of code prints the contents of the foods dictionary to check that the change has been made.

 

You could also update the dictionary in the same way using

 

foods["Sarah"] = "Steak"

 

Deleting a Person

 

What happens if Dexter decides that he no longer wants to be part of the dictionary? Add these lines to your code:

 

del foods["Dexter"]
print(foods)

 

To remove Dexter from the dictionary you use the del statement, which requires the name of the dictionary and the key to be removed. Run this code and you will see that Dexter has been removed from the dictionary.

 

So why is a dictionary useful?

 

Dictionaries offer a simple yet powerful structure. Think back to the quiz game in week 1. The final version of that game had a high score table for ten players. The same high score system could be stored as a dictionary. In this case the player name would be the key, and the value would be their score.

 

highscores = {"Sarah":3,"Li":2,"Anthony":1}

With the high score table as a dictionary like this, without knowing the previous project you could still guess as to what this code does.

Questions

  • When might you want to use a dictionary using keys that are not people’s names?
  • When may you want to use other data types (i.e. not strings or integers) as the values in a dictionary?

 

This article is from the free online

Programming 103: Saving and Structuring Data

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now