Dictionaries are a way to store information that is connected in some way. Dictionaries store information in key-value pairs, so that any one piece of information in a dictionary is connected to at least one other piece of information.
Dictionaries do not store their information in any particular order, so you may not get your information back in the same order you entered it.
In [ ]:
dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3}
Since the keys and values in dictionaries can be long, we often write just one key-value pair on a line. You might see dictionaries that look more like this:
In [ ]:
dictionary_name = {key_1: value_1,
key_2: value_2,
key_3: value_3,
}
This is a bit easier to read, especially if the values are long.
In [2]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
We can get individual items out of the dictionary, by giving the dictionary's name, and the key in square brackets:
In [7]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
print("\nWord: %s" % 'list')
print("Meaning: %s" % python_words['list'])
print("\nWord: %s" % 'dictionary')
print("Meaning: %s" % python_words['dictionary'])
print("\nWord: %s" % 'function')
print("Meaning: %s" % python_words['function'])
This code looks pretty repetitive, and it is. Dictionaries have their own for-loop syntax, but since there are two kinds of information in dictionaries, the structure is a bit more complicated than it is for lists. Here is how to use a for loop with a dictionary:
In [7]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Print out the items in the dictionary.
for word, meaning in python_words.items():
print("\nWord: %s" % word)
print("Meaning: %s" % meaning)
The output is identical, but we did it in 3 lines instead of 6. If we had 100 terms in our dictionary, we would still be able to print them out with just 3 lines.
The only tricky part about using for loops with dictionaries is figuring out what to call those first two variables. The general syntax for this for loop is:
In [ ]:
for key_name, value_name in dictionary_name.items():
print(key_name) # The key is stored in whatever you called the first variable.
print(value_name) # The value associated with that key is stored in your second variable.
In [1]:
# Create an empty dictionary.
python_words = {}
# Fill the dictionary, pair by pair.
python_words['list'] ='A collection of values that are not connected, but have an order.'
python_words['dictionary'] = 'A collection of key-value pairs.'
python_words['function'] = 'A named set of instructions that defines a set of actions in Python.'
# Print out the items in the dictionary.
for word, meaning in python_words.items():
print("\nWord: %s" % word)
print("Meaning: %s" % meaning)
At some point you may want to modify one of the values in your dictionary. Modifying a value in a dictionary is pretty similar to modifying an element in a list. You give the name of the dictionary and then the key in square brackets, and set that equal to the new value.
In [17]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
print('dictionary: ' + python_words['dictionary'])
# Clarify one of the meanings.
python_words['dictionary'] = 'A collection of key-value pairs. Each key can be used to access its corresponding value.'
print('\ndictionary: ' + python_words['dictionary'])
You may want to remove some key-value pairs from one of your dictionaries at some point. You can do this using the same del
command you learned to use with lists. To remove a key-value pair, you give the del
command, followed by the name of the dictionary, with the key that you want to delete. This removes the key and the value as a pair.
In [2]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Show the current set of words and meanings.
print("\n\nThese are the Python words I know:")
for word, meaning in python_words.items():
print("\nWord: %s" % word)
print("Meaning: %s" % meaning)
# Remove the word 'list' and its meaning.
del python_words['list']
# Show the current set of words and meanings.
print("\n\nThese are the Python words I know:")
for word, meaning in python_words.items():
print("\nWord: %s" % word)
print("Meaning: %s" % meaning)
If you were going to work with this code, you would certainly want to put the code for displaying the dictionary into a function. Let's see what this looks like:
In [3]:
###highlight=[2,3,4,5,6,7,8,16,21]
def show_words_meanings(python_words):
# This function takes in a dictionary of python words and meanings,
# and prints out each word with its meaning.
print("\n\nThese are the Python words I know:")
for word, meaning in python_words.items():
print("\nWord: %s" % word)
print("Meaning: %s" % meaning)
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
show_words_meanings(python_words)
# Remove the word 'list' and its meaning.
del python_words['list']
show_words_meanings(python_words)
As long as we have a nice clean function to work with, let's clean up our output a little:
In [4]:
###highlight=[7]
def show_words_meanings(python_words):
# This function takes in a dictionary of python words and meanings,
# and prints out each word with its meaning.
print("\n\nThese are the Python words I know:")
for word, meaning in python_words.items():
print("\n%s: %s" % (word, meaning))
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
show_words_meanings(python_words)
# Remove the word 'list' and its meaning.
del python_words['list']
show_words_meanings(python_words)
This is much more realistic code.
Modifying a value in a dictionary was straightforward, because nothing else depends on the value. Modifying a key is a little harder, because each key is used to unlock a value. We can change a key in two steps:
Here's what this looks like. We will use a dictionary with just one key-value pair, to keep things simple.
In [37]:
# We have a spelling mistake!
python_words = {'lisst': 'A collection of values that are not connected, but have an order.'}
# Create a new, correct key, and connect it to the old value.
# Then delete the old key.
python_words['list'] = python_words['lisst']
del python_words['lisst']
# Print the dictionary, to show that the key has changed.
print(python_words)
Since dictionaries are really about connecting bits of information, you will often use them in the ways described above, where you add key-value pairs whenever you receive some new information, and then you retrieve the key-value pairs that you care about. Sometimes, however, you will want to loop through the entire dictionary. There are several ways to do this:
In [8]:
my_dict = {'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3',
}
for key, value in my_dict.items():
print('\nKey: %s' % key)
print('Value: %s' % value)
This works because the method .items()
pulls all key-value pairs from a dictionary into a list of tuples:
In [10]:
my_dict = {'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3',
}
print(my_dict.items())
The syntax for key, value in my_dict.items():
does the work of looping through this list of tuples, and pulling the first and second item from each tuple for us.
There is nothing special about any of these variable names, so Python code that uses this syntax becomes really readable. Rather than create a new example of this loop, let's just look at the original example again to see this in a meaningful context:
In [11]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
for word, meaning in python_words.items():
print("\nWord: %s" % word)
print("Meaning: %s" % meaning)
In [13]:
my_dict = {'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3',
}
for key in my_dict.keys():
print('Key: %s' % key)
This is actually the default behavior of looping through the dictionary itself. So you can leave out the .keys()
part, and get the exact same behavior:
In [14]:
###highlight=[7]
my_dict = {'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3',
}
for key in my_dict:
print('Key: %s' % key)
The only advantage of using the .keys()
in the code is a little bit of clarity. But anyone who knows Python reasonably well is going to recognize what the second version does. In the rest of our code, we will leave out the .keys()
when we want this behavior.
You can pull out the value of any key that you are interested in within your loop, using the standard notation for accessing a dictionary value from a key:
In [17]:
###highlight=[9,10]
my_dict = {'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3',
}
for key in my_dict:
print('Key: %s' % key)
if key == 'key_2':
print(" The value for key_2 is %s." % my_dict[key])
Let's show how we might use this in our Python words program. This kind of loop provides a straightforward way to show only the words in the dictionary:
In [20]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Show the words that are currently in the dictionary.
print("The following Python words have been defined:")
for word in python_words:
print("- %s" % word)
We can extend this slightly to make a program that lets you look up words. We first let the user choose a word. When the user has chosen a word, we get the meaning for that word, and display it:
In [2]:
###highlight=[12,13,14]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Show the words that are currently in the dictionary.
print("The following Python words have been defined:")
for word in python_words:
print("- %s" % word)
# Allow the user to choose a word, and then display the meaning for that word.
requested_word = raw_input("\nWhat word would you like to learn about? ")
print("\n%s: %s" % (requested_word, python_words[requested_word]))
This allows the user to select one word that has been defined. If we enclose the input part of the program in a while loop, the user can see as many definitions as they'd like:
In [4]:
###highlight=[12,13,14,15,16,17,18,19,20]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Show the words that are currently in the dictionary.
print("The following Python words have been defined:")
for word in python_words:
print("- %s" % word)
requested_word = ''
while requested_word != 'quit':
# Allow the user to choose a word, and then display the meaning for that word.
requested_word = raw_input("\nWhat word would you like to learn about? (or 'quit') ")
if requested_word in python_words.keys():
print("\n %s: %s" % (requested_word, python_words[requested_word]))
else:
# Handle misspellings, and words not yet stored.
print("\n Sorry, I don't know that word.")
This allows the user to ask for as many meanings as they want, but it takes the word "quit" as a requested word. Let's add an elif
clause to clean up this behavior:
In [6]:
###highlight=[16,17,18,19,20,21,22,23,24]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Show the words that are currently in the dictionary.
print("The following Python words have been defined:")
for word in python_words:
print("- %s" % word)
requested_word = ''
while requested_word != 'quit':
# Allow the user to choose a word, and then display the meaning for that word.
requested_word = raw_input("\nWhat word would you like to learn about? (or 'quit') ")
if requested_word in python_words.keys():
# This is a word we know, so show the meaning.
print("\n %s: %s" % (requested_word, python_words[requested_word]))
elif requested_word != 'quit':
# This is not in python_words, and it's not 'quit'.
print("\n Sorry, I don't know that word.")
else:
# The word is quit.
print "\n Bye!"
In [15]:
my_dict = {'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3',
}
for value in my_dict.values():
print('Value: %s' % value)
We can use this loop syntax to have a little fun with the dictionary example, by making a little quiz program. The program will display a meaning, and ask the user to guess the word that matches that meaning. Let's start out by showing all the meanings in the dictionary:
In [16]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
for meaning in python_words.values():
print("Meaning: %s" % meaning)
Now we can add a prompt after each meaning, asking the user to guess the word:
In [2]:
###highlight=[12,13,14,15,16,17,18]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Print each meaning, one at a time, and ask the user
# what word they think it is.
for meaning in python_words.values():
print("\nMeaning: %s" % meaning)
guessed_word = raw_input("What word do you think this is? ")
# The guess is correct if the guessed word's meaning matches the current meaning.
if python_words[guessed_word] == meaning:
print("You got it!")
else:
print("Sorry, that's just not the right word.")
This is starting to work, but we can see from the output that the user does not get the chance to take a second guess if they guess wrong for any meaning. We can use a while loop around the guessing code, to let the user guess until they get it right:
In [20]:
###highlight=[12,13,14,15,16,17,18,19,20,21,22]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
# Print each meaning, one at a time, and ask the user
# what word they think it is.
for meaning in python_words.values():
print("\nMeaning: %s" % meaning)
# Assume the guess is not correct; keep guessing until correct.
correct = False
while not correct:
guessed_word = input("\nWhat word do you think this is? ")
# The guess is correct if the guessed word's meaning matches the current meaning.
if python_words[guessed_word] == meaning:
print("You got it!")
correct = True
else:
print("Sorry, that's just not the right word.")
This is better. Now, if the guess is incorrect, the user is caught in a loop that they can only exit by guessing correctly. The final revision to this code is to show the user a list of words to choose from when they are asked to guess:
In [8]:
###highlight=[7,8,9,10,11,12,23,24,25]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
def show_words(python_words):
# A simple function to show the words in the dictionary.
display_message = ""
for word in python_words.keys():
display_message += word + ' '
print display_message
# Print each meaning, one at a time, and ask the user
# what word they think it is.
for meaning in python_words.values():
print("\n%s" % meaning)
# Assume the guess is not correct; keep guessing until correct.
correct = False
while not correct:
print("\nWhat word do you think this is?")
show_words(python_words)
guessed_word = raw_input("- ")
# The guess is correct if the guessed word's meaning matches the current meaning.
if python_words[guessed_word] == meaning:
print("You got it!")
correct = True
else:
print("Sorry, that's just not the right word.")
Dictionaries are quite useful because they allow bits of information to be connected. One of the problems with dictionaries, however, is that they are not stored in any particular order. When you retrieve all of the keys or values in your dictionary, you can't be sure what order you will get them back. There is a quick and easy way to do this, however, when you want them in a particular order.
Let's take a look at the order that results from a simple call to dictionary.keys():
In [2]:
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
for word in python_words.keys():
print(word)
The resulting list is not in order. The list of keys can be put in order by passing the list into the sorted() function, in the line that initiates the for loop:
In [3]:
###highlight=[7]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
for word in sorted(python_words.keys()):
print(word)
This approach can be used to work with the keys and values in order. For example, the words and meanings can be printed in alphabetical order by word:
In [8]:
###highlight=[8]
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
for word in sorted(python_words.keys()):
print("%s: %s" % (word.title(), python_words[word]))
In this example, the keys have been put into alphabetical order in the for loop only; Python has not changed the way the dictionary is stored at all. So the next time the dictionary is accessed, the keys could be returned in any order. There is no way to permanently specify an order for the items in an ordinary dictionary, but if you want to do this you can use the OrderedDict structure.
Nesting is one of the most powerful concepts we have come to so far. Nesting involves putting a list or dictionary inside another list or dictionary. We will look at two examples here, lists inside of a dictionary and dictionaries inside of a dictionary. With nesting, the kind of information we can model in our programs is expanded greatly.
A dictionary connects two pieces of information. Those two pieces of information can be any kind of data structure in Python. Let's keep using strings for our keys, but let's try giving a list as a value.
The first example will involve storing a number of people's favorite numbers. The keys consist of people's names, and the values are lists of each person's favorite numbers. In this first example, we will access each person's list one at a time.
In [20]:
# This program stores people's favorite numbers, and displays them.
favorite_numbers = {'eric': [3, 11, 19, 23, 42],
'ever': [2, 4, 5],
'willie': [5, 35, 120],
}
# Display each person's favorite numbers.
print("Eric's favorite numbers are:")
print(favorite_numbers['eric'])
print("\nEver's favorite numbers are:")
print(favorite_numbers['ever'])
print("\nWillie's favorite numbers are:")
print(favorite_numbers['willie'])
We are really just working our way through each key in the dictionary, so let's use a for loop to go through the keys in the dictionary:
In [7]:
###highlight=[8,9,10,11]
# This program stores people's favorite numbers, and displays them.
favorite_numbers = {'eric': [3, 11, 19, 23, 42],
'ever': [2, 4, 5],
'willie': [5, 35, 120],
}
# Display each person's favorite numbers.
for name in favorite_numbers:
print("\n%s's favorite numbers are:" % name.title())
print(favorite_numbers[name])
This structure is fairly complex, so don't worry if it takes a while for things to sink in. The dictionary itself probably makes sense; each person is connected to a list of their favorite numbers.
This works, but we'd rather not print raw Python in our output. Let's use a for loop to print the favorite numbers individually, rather than in a Python list.
In [13]:
###highlight=[11,12,13,14]
# This program stores people's favorite numbers, and displays them.
favorite_numbers = {'eric': [3, 11, 19, 23, 42],
'ever': [2, 4, 5],
'willie': [5, 35, 120],
}
# Display each person's favorite numbers.
for name in favorite_numbers:
print("\n%s's favorite numbers are:" % name.title())
# Each value is itself a list, so we need another for loop
# to work with the list.
for favorite_number in favorite_numbers[name]:
print(favorite_number)
Things get a little more complicated inside the for loop. The value is a list of favorite numbers, so the for loop pulls each favorite_number out of the list one at a time. If it makes more sense to you, you are free to store the list in a new variable, and use that to define your for loop:
In [15]:
###highlight=[12,13,14,15]
# This program stores people's favorite numbers, and displays them.
favorite_numbers = {'eric': [3, 11, 19, 23, 42],
'ever': [2, 4, 5],
'willie': [5, 35, 120],
}
# Display each person's favorite numbers.
for name in favorite_numbers:
print("\n%s's favorite numbers are:" % name.title())
# Each value is itself a list, so let's put that list in a variable.
current_favorite_numbers = favorite_numbers[name]
for favorite_number in current_favorite_numbers:
print(favorite_number)
The most powerful nesting concept we will cover right now is nesting a dictionary inside of a dictionary.
To demonstrate this, let's make a dictionary of pets, with some information about each pet. The keys for this dictionary will consist of the pet's name. The values will include information such as the kind of animal, the owner, and whether the pet has been vaccinated.
In [24]:
# This program stores information about pets. For each pet,
# we store the kind of animal, the owner's name, and
# the breed.
pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True},
'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False},
'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True},
}
# Let's show all the information for each pet.
print("Here is what I know about Willie:")
print("kind: " + pets['willie']['kind'])
print("owner: " + pets['willie']['owner'])
print("vaccinated: " + str(pets['willie']['vaccinated']))
print("\nHere is what I know about Walter:")
print("kind: " + pets['walter']['kind'])
print("owner: " + pets['walter']['owner'])
print("vaccinated: " + str(pets['walter']['vaccinated']))
print("\nHere is what I know about Peso:")
print("kind: " + pets['peso']['kind'])
print("owner: " + pets['peso']['owner'])
print("vaccinated: " + str(pets['peso']['vaccinated']))
Clearly this is some repetitive code, but it shows exactly how we access information in a nested dictionary. In the first set of print
statements, we use the name 'willie' to unlock the 'kind' of animal he is, the 'owner' he has, and whether or not he is 'vaccinated'. We have to wrap the vaccination value in the str
function so that Python knows we want the words 'True' and 'False', not the values True
and False
. We then do the same thing for each animal.
Let's rewrite this program, using a for loop to go through the dictionary's keys:
In [12]:
###highlight=[10,11,12,13,14,15]
# This program stores information about pets. For each pet,
# we store the kind of animal, the owner's name, and
# the breed.
pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True},
'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False},
'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True},
}
# Let's show all the information for each pet.
for pet_name, pet_information in pets.items():
print("\nHere is what I know about %s:" % pet_name.title())
print("kind: " + pet_information['kind'])
print("owner: " + pet_information['owner'])
print("vaccinated: " + str(pet_information['vaccinated']))
This code is much shorter and easier to maintain. But even this code will not keep up with our dictionary. If we add more information to the dictionary later, we will have to update our print statements. Let's put a second for loop inside the first loop in order to run through all the information about each pet:
In [13]:
###highlight=[14,15]
# This program stores information about pets. For each pet,
# we store the kind of animal, the owner's name, and
# the breed.
pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True},
'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False},
'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True},
}
# Let's show all the information for each pet.
for pet_name, pet_information in pets.items():
print("\nHere is what I know about %s:" % pet_name.title())
# Each animal's dictionary is in 'information'
for key in pet_information:
print(key + ": " + str(pet_information[key]))
This nested loop can look pretty complicated, so again, don't worry if it doesn't make sense for a while.
Let's show one last version that uses some if statements to clean up our data for printing:
In [14]:
###highlight=[15,16,17,18,19,20,21,22,23,24,25,26,27]
# This program stores information about pets. For each pet,
# we store the kind of animal, the owner's name, and
# the breed.
pets = {'willie': {'kind': 'dog', 'owner': 'eric', 'vaccinated': True},
'walter': {'kind': 'cockroach', 'owner': 'eric', 'vaccinated': False},
'peso': {'kind': 'dog', 'owner': 'chloe', 'vaccinated': True},
}
# Let's show all the information for each pet.
for pet_name, pet_information in pets.items():
print("\nHere is what I know about %s:" % pet_name.title())
# Each animal's dictionary is in pet_information
for key in pet_information:
if key == 'owner':
# Capitalize the owner's name.
print(key + ": " + pet_information[key].title())
elif key == 'vaccinated':
# Print 'yes' for True, and 'no' for False.
vaccinated = pet_information['vaccinated']
if vaccinated:
print 'vaccinated: yes'
else:
print 'vaccinated: no'
else:
# No special formatting needed for this key.
print(key + ": " + pet_information[key])
This code is a lot longer, and now we have nested if statements as well as nested for loops. But keep in mind, this structure would work if there were 1000 pets in our dictionary, and it would work if we were storing 1000 pieces of information about each pet. One level of nesting lets us model an incredible array of information.
While one level of nesting is really useful, nesting much deeper than that gets really complicated, really quickly. There are other structures such as classes which can be even more useful for modeling information. In addition to this, we can use Python to store information in a database, which is the proper tool for storing deeply nested information.
Often times when you are storing information in a database you will pull a small set of that information out and put it into a dictionary, or a slightly nested structure, and then work with it. But you will rarely, if ever, work with Python data structures nested more than one level deep.