3.1. Working with Dictionaries

A dictionary is a collection of key & value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key. A key can be a number, string, or whatever you want.

A dictionary is defined using braces {}. Inside the dictionary, it consists of key & value in this format: key1 : value1 where multiple entries are separated by comma ,.

icecream_1 = {'displayed name': 'Ice Cream Super Creamy', 'price': 200}

A key-value pair is a set of values associated with each other. A dictionary can have one key-value pair like this:

icecream_1 = {'displayed name': 'Ice Cream Super Creamy'}

This dictionary have one piece of information about ice_cream, that is the ice cream's displayed name. The string displayed name is a key in this dictionary and the value pair is Ice Cream Super Creamy.

Getting Values in a Dictionary

The basic conditional test checks whether the value of a variable is equal to the value we want. Let's see the following example.

By using that, we can make a list of someone order by combining the values inside the dictionary:

Let's expand this by adding another dictionary and a `for` loop. Notice the pattern.


Adding New Key-Value Pairs

You can add a new key-value pairs to a dictionary any time. We will add an additional information to ice_cream, that is flavor.

> We define the dictionary in line ❶ like before. At ❷, we add a new key-value pair to that dictionary: key flavor and value vanilla. When we print the second time, we see that the new pair is added to our dictionary.

Starting with Empty Dictionary

It can be convenient to start with an empty dictionary. To do this, start with {} (an empty set of braces) then add each key-value pair.

> In this code, we define an empty dictionary ice_cream_1 and then add price and flavor to it. The results is a dictionary with two new key-value pair.

Changing the Value in Dictionary

To change a value in a dictionary, you can simply assign new value to the key-value pair in square brackets like this:

Removing Key-Value Pairs

When you no longer need some information in a dictionary, you can use the del statement to permanently delete a key-value pair like this: del key[value]. Here's an example:

> Be aware that the deleted key-value pair cannot be retrieved again.

Using Dictionary for Similar Objects

Dictionary can also be used to store one kind of information about many items. Say you have a variable price that contains a lot of item like this:

> In this example, we break a larger dictionary into several lines. Each key is the name of the foods, and the value is their respective prices.
Writing a dictionary like the above example is a good practice and its easier to read: Press ENTER after the opening brace and after each commas. The indentation will be automatically generated.

We can use the above dictionary as in the following example. You could use this syntax with any item stored in the dictionary.

Using get( ) to Access Value

Using keys and square brackets can cause a problem if the key you ask for does not exist. See the following example.

It will gives the KeyError, which means that Python could not find the key that you asked for, which is flavor.

For dictionaries, you can use the get() method to set a default value that will be given if the key does not exist. This method requires a key as a first argument, and the second optional argument is the value to be returned if the key doesn't exist:

In the above example, we have three cases:

  1. If the key does not exist, get() will give the default option (the second argument).

  2. If the key does exist, get() will do the same thing as using []

  3. If the key does not exist and no second argument, get() will return the value None.

Exercise 3.1

  1. Person
    Andy Johansson is a 32-year old programmer who lives in Austin, Texas.
    Use a dictionary to store information about that person. Store his first name, last name, age, occupation, and the city which he lives in. Then, print the following:


First Name : ANDY
Last Name : JOHANSSON
Age : 32
Occupation : PROGRAMMER
Home City : AUSTIN
Home State : TEXAS


  1. Favorite Number
    Use a dictionary to store people's favorite numbers. Think of five names, and use them as keys in your dictionary. Think of a favorite number for each person, and store each as a value in your dictionary (no need to use for loop). Print :
    {name}'s favorite number is {favorite number}.