Dictionaries
Understanding dictionaries allows you to model a variety of real-world objects more accurately. For example, we could create a dictionary representing a person and then store as much information as we want about that person. You can store their name, location, age, profession, and any other aspect of a person you can describe. We can store any two kinds of information that can be matched up, such as a list of words and their meanings, a list of people's names and their favorite numbers, a list of mountains and their elevations, etc.
A Simple Dictionary
Consider a game featuring aliens that can have different colors and point values. This simple dictionary stores information about a particular alien:
1 2 3 4 |
|
alien_0
stores the alien's color and point value. The last two lines access and display that information, as shown here:green
5
Working with Dictionaries
A dictionary in Python 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's value can be a number, a string, a list, or even another dictionary. In fact, you can use any object that you can create in Python as a value in a dictionary.
In Python, a dictionary is wrapped in braces, { }, with a series of key-value pairs inside the braces, as shown in the earlier example:
alien_0 = {'color': 'green', 'points': 5}
The simplest dictionary has exactly one key-value pair, as shown in this modified version of the alien_0
dictionary:
alien_0 = {'color': 'green'}
alien_0
, namely the alien's color. The string 'color'
is a key in this dictionary, and its associated value is 'green'
. Accessing Values in a Dictionary
To get the value associated with a key, give the name of the dictionary and then place the key inside a set of square brackets, as shown here:
1 2 |
|
'color'
from the dictionary alien_0
: green
alien_0
dictionary with two key-value pairs: alien_0 = {'color': 'green', 'points': 5}
alien_0
. If a player shoots down this alien, you can look up how many points they should earn using code like this:1 2 3 4 |
|
'points'
from the dictionary. This value is then assigned to the variable 'new_points'
. The code on line 4 then prints a statement about how many points the player just earned. Adding New Key-Value Pairs
Dictionaries are dynamic structures, and you can add new key-value pairs to a dictionary at any time. For example, to add a new key-value pair, you would give the name of the dictionary followed by the new key in square brackets along with the new value.
Let's add two new pieces to the alien_0
dictionary: the alien's x- and y- coordinates, which will help us display the alien in a particular position on the screen. Let's place the alien on the left edge of the screen, 25 pixels down from the top. Because screen coordinates usually start at the upper-left corner of the screen, we'll place the alien on the left edge of the screen by setting the x-coordinate to 0 and 25 pixels from the top by setting the y-coordinate to positive 25:
1 2 3 4 5 6 |
|
'x_position'
and a value of 0
. We do the same for key 'y_position'
on the next line. Finally, when we print the modified dictionary, we see the two additional key-value pairs:{'color': 'green', 'points': '5'}
{'color': 'green', 'points': '5', 'x_position': 0, 'y_position': 25}
Starting with an Empty Dictionary
It's sometimes convenient, or even necessary, to start with an empty dictionary and then add each new item to it. To start filling an empty dictionary, define a dictionary with an empty set of braces and then add each key-value pair on its own line. For example, here's how to build the alien_0
dictionary using this approach:
1 2 3 4 5 6 |
|
alien_0
dictionary, and then add color and point values to it. The result is the dictionary we have been usinging in previous examples:{'color': 'green', 'points': '5'}
Modifying Values ina Dictionary
To modify a value in a dictionary, give the name of the dictionary with the key in square brackets and then the new value you want associated with that key. For example, consider an alien that changes from green to yellow as a game progresses:
1 2 3 4 5 |
|
alien_0
that contains only the aliens color; then we change the value associated with the key 'color'
to 'yellow'
. The output shows that the alien has indeed changed from green to yellow:The alien is green.
The alien is now yellow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
'medium'
. We also print the original value of x_position
to see how far the alien moves to the right. On line 5, an if-elif-else
chain determins how far the alien should move to the right and assigns this value to the variable x_increment
. If the alien's speed is 'slow'
, it moves one unit, if it is 'medium'
, it moves two, and if it is 'fast'
, it moves three. Once the increment has been calculated, it is added to the value of x_position
on line 13 and the result is stored in the dictionary's x_position
.
Because this is a medium-speed alien, its position shifts two units to the right:
Original position: 0
New position: 2
alien_0['speed'] = 'fast'
if-elif-else block
would then assign a larger value to x_increment
the next time the code runs. Removing Key-Value Pairs
When you no longer need a piece of information that is stored ina dictionary, you can you the del
statement to completely remove a key-value pair. All del
needs is the name of the dictionary and the key that you want to remove.
For example, let's remove the key 'points'
from the alien_0
dictionary along with its value:
1 2 3 4 5 |
|
'points'
from the dictionary alien_0
and to remove the value associated with that key as well. The output shows that the key 'points'
and its value of 5 are deleted from the dictionary, and the rest is unaffected:{'color': 'green', 'points': 5}
{'color': 'green'}
A Dictionary of Similar Objects
The previous example involved storing different kinds of information about one object, an alien in a game. You can also use a dictionary to store one kind of information about many objects. For example, a dictionary of a group of people's favorite programming language. A dictionary for this could look like:
1 2 3 4 5 6 |
|
ENTER
after the opening brace. Then indent the next line one tab, and write your first key-value pair, followed by a comma. Once you have finished defining the dictionary, add a closing brace on a new line after the last key-value pair and indent it one level so it aligns with the keys in the dictionary. It's good practice to include a comma after the last key-value pair, just in case you want to add more in the future.
To use this dictionary, given the name of a person, you can easily look up their favorite language:
1 2 3 4 5 6 7 8 9 |
|
favorite_languages['sarah']
language
. Creating a new variable here makes for a much cleaner print()
call. The output shows Sarah's favorite Language: Sarah's favorite langugage is C.
Using get()
to Access Values
Using keys in square brackets to retrieve the value you're interested in from a dictionary might cause one potential problem: if the key you ask for doesn't exist, you'll get an error.
Let's see what happens when you ask for the point value of an alien that doesn't have a point value set:
1 2 |
|
KeyError Traceback (most recent call last)
<ipython-input-9-3914d637e9f3> in <module>
1 alien_0 = {'color': 'green', 'speed': 'slow'}
----> 2 print(alien_0['points'])
KeyError: 'points'
get()
method to set a default value that will be returned if the requested key doesn't exist. The get()
method requires a key as a first argument. As a second optional argument, you can pass the value to be returned if the key doesn't exist:
1 2 3 4 |
|
'points'
exists in the dictionary, you'll get corresponding value. If it doesn't, you get the default value. In this case, points
doesn't exist, and we get a clean message instead of an error: No point value assigned.
get()
method instead of the square bracket notation. Looping Through a Dictionary
A single Python dictionary can contain just a few key-value pairs or millions of pairs. Because a dictionary can contain large amounts of data, Python lets you loop through a dictionary. Dictionaries can be used to store information in a variety of ways; therefore, several different ways exist to loop through them. You can loop through all of a dictionary's key-value pairs, through its keys, or its values.
Looping Through All Key-Value Pairs
Before we explore the different approaches to looping, let's consider a new dictionary designed to store information about a user on website. The following dictionary would store one person's username, first name, and last name:
1 2 3 4 5 |
|
user_0
based on what you've already learned in this chapter. But what if you wanted to see everything stored in this user's dictionary? To do so, you could loop through the dictionary using a for
loop. 1 2 3 4 5 6 7 8 9 |
|
for
loop for a dictionary, you create names for the two variables that will hold the key and value in each key-value pair. You can choose any names you want for these two variables. This code would work just as well if you had used abbreviated for the variable names, like this: for k, v in user_0.items()
for
statement on line 7 includes the name of the dictionary followed by the method items()
, which returns a list of key-value pairs. The for
loop then assigns each of these pairs to the two vairbales provided. In the preceding example, we use the variables to print each key (line 8), followed by the associated value (line 9). The "\n" in the first print()
call ensures that a blank line is inserted bfore each key-value pair in the output:Key: username
Value: efermi
Key: first
Value: enrico
Key: last
Value: fermi
favorite_languages
example from above, which stores the same kind of information for many different keys. If you loop through the favorite_languages
dictionary, you get the name of each person in the dictionary and their favorite programming language. Because the keys always refer to a person's name and the value is always language, we'll use the variables name
and language
in the loop, instead of key
and value
. This will make it easier to follow what's happening inside the loop:1 2 3 4 5 6 7 8 9 |
|
name
, and the value is assigned to the variable language
. These descriptive names make it much easier to see what the print()
call on line 9 is doing. Now, in just a few lines of code, we can display all the information from the poll:
Jen's favorite language is Python!
Sarah's favorite language is C!
Edward's favorite language is Ruby!
Phil's favorite language is Python!
Looping Through All The Keys in a Dictionary
The keys()
method is useful when you don't need to work with all of the values in a dictionary. Let's loop through the favorite_languages
dictionary and print the names of everyone who answered:
1 2 3 4 5 6 7 8 9 |
|
favorite_languages
and assign them one at a time to the variable name
. The output shows the names of everyone who answered:Jen
Sarah
Edward
Phil
for name in favorite_languages:
for name in favorite_languages.keys():
keys()
method explicitly if it makes your code easier to read, or you can omit it if you wish. You can access the value associated with any key you care about inside the loop by using the current key. Let's print a message to a couple of friends about the languages they chose. We'll loop through the names in the dictionary as we did previously, but when the name matches on of our friends, we'll display a message about their favorite language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
name
we're working with is in the list friends
. If it is, we determine the person's favorite language using the name of the dictionary and the current value of name
as the key (line 13). We then print a special greeting, including a reference to their language of choice. Everyone's name is printed, but our friends receive a special message:
Hi, Jen.
Hi, Sarah.
Sarah, I see you love C!
Hi, Edward.
Hi, Phil.
Phil, I see you love Python!
keys()
method to find out if a particular person was polled. This time, let's find out if Erin took the poll: 1 2 3 4 5 6 7 8 9 |
|
keys()
method isn't just for looping: it actually returns a list of all the keys, and the code on line 8 simply checks if 'erin'
is in the list. Because she is not, a message is printed inviting her to take the poll. Looping Through a Dictionary's Keys in a Particular Order
Starting in Python 3.7, looping through a dictionary returns the items in the same order they were inserted. Sometimes, though, you'll want to loop through a dictionary in a different order.
One way to do this is to sort the keys as they're retunred in the for
loop. You can use the sorted()
function to get a copy of the keys in order:
1 2 3 4 5 6 7 8 9 |
|
for
statement is like other for
statements except that we've wrapped the sorted()
function around the dictionary.keys()
method. This tells Python to list all keys in the dictionary and sort that list before looping through it. The output shows everyone who took the poll, with the names displayed in order:Edward, thank you for taking the poll.
Jen, thank you for taking the poll.
Phil, thank you for taking the poll.
Sarah, thank you for taking the poll.
Looping Through All Values in a Dictionary
If you are primarily interested in the values that a dictionary contains, you can use the values()
method to return a list of values without any keys. For example, say we simply want a list of all languages chosen in our programming language poll without the name of the person who chose each language:
1 2 3 4 5 6 7 8 9 10 |
|
for
statement here pulls each value from the dictionary and assigns it to the variable language
. When these values are printed, we get a list of all chosen languages: The following languages have been mentioned:
Python
C
Ruby
Python
for
loop in place of the one used above:for language in set(favorite_languages.values()):
set()
around a list that contains duplicate items, Python identifies the unique items in the list and build a set from those items. The result is a nonrepetitive list of languages that have been menetioned:The following languages have been mentioned:
C
Ruby
Python
Nesting
Sometimes you'll want to store multiple dictionaries in a list, or a list of items as a value in a dictionary. This is called nesting. You can nest dictionaries inside a list, a list of items inside a dictionary, or even a dictionary inside another dictionary. Nesting is a powerful feature, as the following examples will demonstrate.
A List of Dictionaries
The alien_0
dictionary contains a variety of information about one alien, but it has no room to store information about a second alien, much less a full screen of aliens. How can you managae a fleet of aliens? One way is to make a list of aliens in which each alien is a dictionary of information about that alien. For example, the following code builds a list of three aliens:
1 2 3 4 5 6 7 8 |
|
aliens
. Finally, we loop through the list and print out each alien: {'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}
range()
to create a fleet of 30 aliens: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
range()
returns a series of numbers, which just tells Python how many times we want the loop to repeat. Each time the loop runs we create a new alien (line 6) and then append each new alien to the list aliens
(line 7). On line 10 we us a slice to print the first five aliens, and then on line 15 we print the length of the list to prove we've actually generated the full fleet of 30 aliens: {'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
...
Total number of aliens: 30
How might you work with a group of aliens like this? Imagine that one aspect of a game has some aliens changing color and moving faster as the game progress. When it's time to change colors, we can use a for
loop and an if
statement to change the color of the aliens. For example, to change the first three aliens to yellow, medium-speed aliens worth 10 points each, we could do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
if
statement to make sure we're only modifying green aliens. If the alien is green, we change the color to 'yellow'
, the speed to 'medium'
, and the point value to 10
, as shown in the following output: {'color': 'yellow', 'points': '10', 'speed': 'medium'}
{'color': 'yellow', 'points': '10', 'speed': 'medium'}
{'color': 'yellow', 'points': '10', 'speed': 'medium'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
...
elif
block that turns yellow aliens into red, fast-moving ones worth 15 points each. Without showing the entire program again, that loop would look like this: 1 2 3 4 5 6 7 8 9 |
|
users
. All of the dictionaries in the list should have an identical structure so you can loop through the list and work with each dictionary object in the same way. A List in a Dictionary
Rather than putting a dictionary inside a list, it's sometimes useful to put a list inside a dictionary. For example, consider how you might describe a pizza that someone is ordering. If you were to use only a list, all you could really store is a list of the pizza's toppings. With a dictionary, a list of toppings can be just one aspect of the pizza you're describing.
In the following example, two kinds of information are stored for each pizza: a type of crust and a list of toppings. The list of toppings is a value associated with the key 'toppings'
. To use the items in the list, we give the name of the dictionary and the key 'toppings'
, as we would any value in the dictionary. Instead of returning a single value, we get a list of toppings:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
'crust'
, and the associated value is the string 'thick'
. The next key, 'toppings'
, has a list as its value that stores all the requested toppings. On line 8 we summarize the order before building the pizza. When you need to break up a long ling in a print()
call, choose an appropriate point at which to break the line being printed and quotation mark. Indent the next line, add an opening quotation mark, and continue the string. Python will automatically combine all of the strings it finds inside the parentheses. To print the toppings, we write a for
loop on line 10. To access the list of toppings, we use the key 'toppings'
, and Python grabs the list of toppings from the dictionary. The following output summarizes the pizza that we plan to build:
You ordered a thick-crust pizza with the following toppings:
mushrooms
extra cheese
for
loop, we use another for
loop to run through the list of languages associated with each person:1 2 3 4 5 6 7 8 9 10 11 |
|
languages
to hold each value from the dictionary, because we know that each value will be a list. Inside the main dictionary loop, we use another for
loop (line 10) to run through each persons list of favorite languages. Now each person can list as many favorite languages as they like:
Jen's favorite languages are:
Python
Ruby
Sarah's favorite languages are:
C
Edward's favorite languages are:
Ruby
Go
Phil's favorite languages are:
Python
Haskell
if
statement at the beginning of the dictionary's for
loop to see whether each person has more than one favorite language by examining the value of len(languages)
. If a person has more than one favoriem the output would stay the same. If the person has only one favorite language, you could change the wording to reflect that. For example, you could say "Sarah's favorite language is C." A Dictionary in a Dictionary
You can nest a dictionary inside another dictionary, b ut your code can get complicated quickly when you do. For example, if you have several users for a website, each with a unique username, you can use the usernames as the keys in a dictionary. You can then store information about each user by using a dictionary as the value associated with their username. In the following listing, we store three pieces of information about each user: their first name, last name, and location. We'll access this information by looping through the usernames and the dictionary of information associated with each username:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
users
with two keys: one each for the usernames 'aeinstein'
and 'mcurie'
. The value associated with each key is a dictionary that includes each user's first name, last name, and location. On line 14 we loop through the users
dictionary. Python assigns each key to the variable username
, and the dictionary associated with each username is assigned to the variable user_info
. Once inside the main dictionary loop, we print the user-name at line 15. On line 16 we start accessing the inner dictionary. The variable user_info
, which contains the dictionary of user information, has three keys: 'first'
, 'last'
, and 'location'
. We use each key to generate a neatly formated full name and location for each person, and then print a summary of what we know about each user:
Username: aeinstein
Full name: Albert Einstein
Location: Princeton
Username: mcurie
Full name: Marie Curie
Location: Paris
for
loop would be more complicated.