Hour 9. Using Dictionaries to Pair Keys with Values


What You’ll Learn in This Hour:

Image How to create a dictionary

Image How to get information about a dictionary

Image How to compare dictionaries

Image When to use dictionaries in the real world


So far, we’ve covered data types that are fairly familiar to most people: numbers (floats and integers), text (strings), and lists. Now, we’re going to cover a data type that may be less familiar: a dictionary.

We’re all familiar with what a dictionary is in the real world: a book with a list of words paired with definitions. A dictionary in Python isn’t so different. In a Python dictionary, you pair keys with values, just like you pair words with definitions. In this kind of dictionary, though, you’re not limited to pairing strings with strings. You can pair any Python value, such as numbers or lists. Even custom data types can be used as a key or value (we’ll get into custom data types in Hour 12, “Expanding Classes to Add Functionality”).

Dictionaries can be incredibly useful when you want to pair up one piece of data with another. For example, you may want to pair a student’s ID with the classes he’s taking, or states with capitals. True, you could do this with a list that contains lists, but dictionaries offer tools specific to this kind of pairing. Also, how long do you think you could remember which index goes with which kind of value? Did you put states first, or capitals?

Creating a Dictionary

Dictionaries are contained in curly brackets. So, if you want to define a dictionary, you just need to set a variable to a pair of empty curly brackets:

>>> states = {}
>>> type(states)
<type 'dict'>

If you already know some values you want to store in the dictionary, then you need to fill in each key/value pair, much like you would enter the items for a list. For each item, the key comes first, followed by a colon, then the value. Each pairing is separated by a comma:

>>> states = {"Virginia": "Richmond", "Maryland": "Annapolis", "New York":
"Albany"}
>>> print states
{'Maryland': 'Annapolis', 'New York': 'Albany', 'Virginia': 'Richmond'}

Most of the time, however, you’re going to need to add keys and values on the fly. Adding a new key (or updating an existing one) looks a bit like changing a value in a list. In this case, however, we’re using a key rather than an index. The new key goes in the brackets ([]) rather than a number:

>>> states = {"Virginia": "Richmond",
...  "Maryland": "Annapolis",
...  "New York": "Albany"}
>>> states['Oregon'] = 'Salem'
>>> states
{'Maryland': 'Annapolis', 'New York': 'Albany', 'Virginia': 'Richmond',
'Oregon': 'Salem'}

Unlike with a list, we don’t get an error if a key doesn’t exist. Python makes room for the new item and adds the key and value pair. What happens if that key already exists, though? Let’s create a dictionary and then use a key that already exists to store a new value.

>>> user_emails = {'15363': '[email protected]'}
>>> user_emails['15363'] = '[email protected]'
>>> user_emails
{'15363': '[email protected]'}

The old value is deleted and the new value is saved as the pair for that key. Keys are unique, so you can only ever have one value for a key.

What if we need to remove a key completely? Even if we set the value for that key to nothing (such as an empty string or zero), that key will still exist. That could wreak havoc with our program. In this case, we’ll need to use the pop() function.

pop() removes a key from a dictionary and returns the value for that key. You don’t need to set that returned value to a variable if you really don’t need it. It won’t hurt your program, and Python will just forget about the value.

Here, we have a dictionary of states and capitals. Let’s remove Virginia.

>>> states = {"Virginia": "Richmond", "Maryland": "Annapolis", "New York":
"Albany"}
>>> states.pop("Virginia")
'Richmond'
>>> states
{'Maryland': 'Annapolis', 'New York': 'Albany'}

If there isn’t a key that matches the value you gave pop(), Python will throw an error. Always make sure that the dictionary has that key before trying to pop it.

Keep in mind that, although all our examples were pairing strings to strings, dictionaries aren’t limited that way. You can pair any data type to any other data type. Feel free to use integers or floats (or any other data type) as keys or values if they serve your needs better, as in the following example:

>>> holidays = {'October': ['Halloween', 'Techies Day', 'Columbus Day']}
>>> birth_years = {1980: 'Katie', 2001: 'Jacob', 2008: 'Hannah'}


Note: Lists and Keys

Lists can’t be used as keys because they’re not hashable. What does that mean? In short, it means there’s no one way to identify a list as unique.


Getting Information About a Dictionary

Getting a value out of a dictionary looks exactly like getting a value out of a list. The variable is followed by the key in square brackets, as follows:

>>> isbns = {'1-4493-2285-9': 'Accessibility Handbook',
...          '0321767349': 'The Python Standard Library by Example',
...          '0596158084': 'Python Pocket Reference'}
>>> isbns['0321767349']
'The Python Standard Library by Example'

If there isn’t a value for that key, Python will throw an error and the program will stop running:

>>> isbns = {'1-4493-2285-9': 'Accessibility Handbook', '0321767349':
'The Python Standard Library by Example', '0596158084': 'Python Pocket Reference'}
>>> isbns['454353543']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '454353543'

But what if you don’t know exactly what keys are in the dictionary at that point in the program? Happily, Python also gives you a function to see if a key is used in a dictionary. To see if an item has used a key, use the has_key() function:

>>> isbns = {'1-4493-2285-9': 'Accessibility Handbook', '0321767349':
'The Python Standard Library by Example', '0596158084': 'Python Pocket Reference'}
>>> isbns.has_key('0321767349')
True
>>> isbns.has_key('9999999999')
False

If the key has been used, has_key() will return True. Otherwise, it will return False.

You can also use the keyword in to see if a key is in a dictionary. If a value is a key in a dictionary, then Python will return True. Otherwise, it will return False.

>>> d = {'one': 1, 'two': 2}
>>> 'one' in d
True
>>> 'three' in d
False

has_key() is not the only helpful function that Python has. There are also functions that give you all the keys being used as well as all the values that have been stored. values() returns a list of all the values used in a dictionary, whereas keys() returns a list of all keys used:

>>> isbns.keys()
['1-4493-2285-9', '0596158084', '0321767349']
>>> isbns.values()
['Accessibility Handbook', 'Python Pocket Reference',
'The Python Standard Library by Example']

Now, you know that keys are unique, but values aren’t. What if there’s more than one of a certain value? As you can see, even if two or more values are the same, they’ll be added to the list returned by values():

>>> favorites = {'Jacob': 'grilled cheese', 'Katie': 'bacon', 'Jim': 'bacon',
'Hannah': 'pink yogurt drink'}
>>> favorites.values()
['grilled cheese', 'pink yogurt drink', 'bacon', 'bacon']

Comparing Dictionaries

When we compared lists, if items weren’t in the exact same order, Python considered those lists to be different. This isn’t the case with dictionaries. Python doesn’t keep the items in a dictionary in a specific order, so it tests to make sure that all the key/value pairs are equal and return True:

>>> d1 = {1: 'one', 2: 'two', 3: 'three'}
>>> d2 = {1: 'one', 2: 'two', 3: 'three'}
>>> d3 = {1: 'one', 3: 'three', 2: 'two'}
>>> d1 == d2
True
>>> d1 == d3
True

If even one of the pairs doesn’t match, the dictionaries aren’t considered equal and return False:

>>> d1 = {1: 'one', 2: 'two', 3: 'three'}
>>> d2 = {1: 'one', 2: 'two', 3: 'Three'}
>>> d1 == d2
False

It’s important to remember that Python doesn’t necessarily keep items in the same order that you enter them. If you need items to be in a specific order, you should use a list.

Using Dictionaries in the Real World

Let’s return to our cook, who probably has the most to gain because her specials would be well suited to being stored in a dictionary. They are, after all, just a pairing between two strings. Could she make the program work even better?

She decides updating the specials every day is getting tiring, so she decides to update her program so that it can hold all the specials for the week. Because she doesn’t change them that often, she can leave the script alone for longer stretches.

She realizes that what she really wants is a dictionary that holds more dictionaries as values. After all, each day could be considered a dictionary (time: special), and the week could be considered a dictionary (day of the week: specials). She double-checks and discovers that, yes, she can store a dictionary as a value in a dictionary! Here’s her whole script:

def get_specials():
    monday = {'B': 'Horseradish omelet. Note: better than it sounds',
              'L': 'Momma's Curry. Note: Can be made spicy.',
              'D': 'Beef brisket. Note: Comes with au jus. That's pronounced "Oh
jhoo", not "Ow Juice"'}
    tuesday = {'B': 'Sausage gravy over biscuits. Note: Toast can be subbed.',
               'L': 'Grilled cheese and tomato soup. Note: We have vegan cheese.',
               'D': 'Meatloaf. Note: Comes with catsup on the top. Not optional.'}
    wednesday = {'B': 'Horseradish omelet. Note: better than it sounds',
              'L': 'Momma's Curry. Note: Can be made spicy.',
              'D': 'Beef brisket. Note: Comes with au jus. That's pronounced "Oh
jhoo", not "Ow Juice"'}
    thursday = {'B': 'Horseradish omelet. Note: better than it sounds',
              'L': 'Momma's Curry. Note: Can be made spicy.',
              'D': 'Beef brisket. Note: Comes with au jus. That's pronounced "Oh
jhoo", not "Ow Juice"'}
    friday = {'B': 'Horseradish omelet. Note: better than it sounds',
              'L': 'Momma's Curry. Note: Can be made spicy.',
              'D': 'Beef brisket. Note: Comes with au jus. That's pronounced "Oh
jhoo", not "Ow Juice"'}
    saturday = {'B': 'Horseradish omelet. Note: better than it sounds',
              'L': 'Momma's Curry. Note: Can be made spicy.',
              'D': 'Beef brisket. Note: Comes with au jus. That's pronounced "Oh
jhoo", not "Ow Juice"'}
    sunday = {'B': 'Horseradish omelet. Note: better than it sounds',
              'L': 'Momma's Curry. Note: Can be made spicy.',
              'D': 'Beef brisket. Note: Comes with au jus. That's pronounced "Oh
jhoo", not "Ow Juice"'}

    specials = {'M': monday,
                'T': tuesday,
                'W': wednesday,
                'R': thursday,
                'F': friday,
                'St': saturday,
                'Sn': sunday}
    return specials

def print_special(special):
    print "The special is:"
    print special
    print "*"*15

def get_day():
    while True:
        day = raw_input("Day (M/T/W/R/F/St/Sn): ")
        if day.upper() in ['M', 'T', 'W', 'R', 'F', 'ST', 'SN']:
            return day.upper()
        else:
            print "I'm sorry, but {} isn't valid.".format(day)


def get_time():
    while True:
        time = raw_input("Time (B/L/D): ")
        if time.upper() in ['B', 'L', 'D']:
            return time.upper()
        else:
            print "I'm sorry, but {} isn't a valid time.".format(time)

def main():
    specials = get_specials()
    print "This script will tell you the specials for any day of the week, and any time."
    while True:
        day = get_day()
        special = specials[day]
        time = get_time()
        print_special(special[time])
        another = raw_input("Do you want to check another day and time? (Y/N)")
        if another.lower() == 'n':
            break

if __name__ == '__main__':
    main()

The largest part of the script, get_specials, sets up all of the specials for the week. This function returns a dictionary of specials for the week. This gives the cook one place to edit all of her specials, if she decides to mix things up by bringing on a new dish.

For the most part, she’ll only be working with one day of the week at a time. She gets the day from the user, the special for that day (using the day as a key), and then the time of the day. She then prints out the special for that specific time.

Summary

During this hour, you learned about dictionaries. A dictionary can be populated with key/value pairs and comes with methods that allow you to get all of the keys or values. You learned that items in a dictionary are not stored in any particular order and that keys are always unique.

Q&A

Q. What’s the deal with “pop”? It seems like an odd term. Why not “remove”?

A. Many times, when languages are being written, lists are written with pop and push functions. This is a bit of programming terminology, usually related to something called stacks. We won’t be going over stacks much in this book, but the Wikipedia article on the topic covers a good deal: http://en.wikipedia.org/wiki/Stack_(abstract_data_type).

Q. Are dictionaries used in all programming languages?

A. Dictionaries aren’t unique, but they’re often called different things in other languages. They might be called hash maps, hash tables, or associative arrays.

Workshop

The Workshop contains quiz questions and exercises to help you solidify your understanding of the material covered. Try to answer all questions before looking at the answers that follow.

Quiz

1. What pairs do you store in dictionaries?

2. Which of the following is true?

a. Keys are always unique.

b. Values are always unique.

c. Both keys and values must be unique.

d. Neither keys nor values have to be unique.

3. How do you get all the keys out of a dictionary?

4. How do you store a new value in a dictionary?

Answers

1. Dictionaries store key/value pairs, where each key is paired to one value.

2. A. Keys are always unique. If you store a new value under an existing key, the current value under that key will be overwritten.

3. The keys() function returns a list of all the keys used in a dictionary.

4. To store a new value in a dictionary, use the variable name, followed by the key in brackets. The format looks like this: dict[key] = newval.

Exercise

Create a program that pairs a student’s name to his class grade. The user should be able to enter as many students as needed and then get a printout of all the students’ names and grades. The output should look like this:

Please give me the name of the student (q to quit): [INPUT]
Please give me their grade: [INPUT]
[And so on...]
Please give me the name of the student (q to quit): q
Okay, printing grades!
Student   Grade
Student1   A
Student2   D
Student3   B
Student4   A
[And so on...]

Carefully consider what should be unique (and therefore used as a key) and what would probably not be unique (and therefore should be stored as a value).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.156.22