Dictionaries in Python

Python provides a dictionary data type that can be used for mapping between arbitrary types. It is like a conventional dictionary, in that it gives you an efficient way to look things up. However, as we see from Table 5-4, it has a much wider range of uses.

To illustrate, we define pos to be an empty dictionary and then add four entries to it, specifying the part-of-speech of some words. We add entries to a dictionary using the familiar square bracket notation:

>>> pos['colorless'] = 'AD]' O >>> pos

{'colorless': 'AD]'} >>> pos['ideas'] = 'N' >>> pos['sleep'] = 'V' >>> pos['furiously'] = 'ADV' >>> pos ©

{'furiously': 'ADV', 'ideas': 'N', 'colorless': 'AD]', 'sleep': 'V'}

So, for example, O says that the part-of-speech of colorless is adjective, or more specifically, that the key 'colorless' is assigned the value 'AD]' in dictionary pos. When we inspect the value of pos © we see a set of key-value pairs. Once we have populated the dictionary in this way, we can employ the keys to retrieve values:

Of course, we might accidentally use a key that hasn't been assigned a value. >>> pos['green']

Traceback (most recent call last):

File "<stdin>", line 1, in ? KeyError: 'green'

This raises an important question. Unlike lists and strings, where we can use len() to work out which integers will be legal indexes, how do we work out the legal keys for a dictionary? If the dictionary is not too big, we can simply inspect its contents by evaluating the variable pos. As we saw earlier in line ©, this gives us the key-value pairs. Notice that they are not in the same order they were originally entered; this is because dictionaries are not sequences but mappings (see Figure 5-3), and the keys are not inherently ordered.

Alternatively, to just find the keys, we can either convert the dictionary to a list O or use the dictionary in a context where a list is expected, as the parameter of sorted() © or in a for loop ©. >>> list(pos) O

['ideas', 'furiously', 'colorless', 'sleep'] >>> sorted(pos) ©

['colorless', 'furiously', 'ideas', 'sleep'] >>> [w for w in pos if w.endswith('s')] © ['colorless', 'ideas']

When you type list(pos), you might see a different order to the one shown here. If you want to see the keys in order, just sort them.

As well as iterating over all keys in the dictionary with a for loop, we can use the for loop as we did for printing lists:

colorless: AD] furiously: ADV sleep: V ideas: N

Finally, the dictionary methods keys(), values(), and items() allow us to access the keys, values, and key-value pairs as separate lists. We can even sort tuples ©, which orders them according to their first element (and if the first elements are the same, it uses their second elements). >>> pos.keys()

['colorless', 'furiously', 'sleep', 'ideas'] >>> pos.values() ['ADJ', 'ADV', 'V', 'N'] >>> pos.items()

[('colorless', 'ADJ'), ('furiously', 'ADV'), ('sleep', 'V'), ('ideas', 'N')] >>> for key, val in sorted(pos.items()): O ... print key + ":", val colorless: ADJ furiously: ADV ideas: N sleep: V

We want to be sure that when we look something up in a dictionary, we get only one value for each key. Now suppose we try to use a dictionary to store the fact that the word sleep can be used as both a verb and a noun:

>>> pos['sleep'] = 'V' >>> pos['sleep'] 'V'

>>> pos['sleep'] = 'N' >>> pos['sleep'] 'N'

Initially, pos['sleep'] is given the value 'V'. But this is immediately overwritten with the new value, 'N' . In other words, there can be only one entry in the dictionary for 'sleep'. However, there is a way of storing multiple values in that entry: we use a list value, e.g., pos['sleep'] = ['N', 'V']. In fact, this is what we saw in Section 2.4 for the CMU Pronouncing Dictionary, which stores multiple pronunciations for a single word.

Was this article helpful?

0 0

Post a comment