Next / Previous / Contents / TCC Help System / NM Tech homepage

5.2. Dictionary methods

A number of useful methods are defined on any Python dictionary. To test whether a key k exists in a dictionary d, use this method:

d.has_key(k)

This is the equivalent of the expression “k in d”: it returns True if the key is in the dictionary, False otherwise.

>>> numberNames
{0: 'zero', 1: 'one', 2: 'two', 5: 'five'}
>>> numberNames.has_key(2)
True
>>> numberNames.has_key(10)
False

To get a list of all the keys in a dictionary d, use this expression:

d.keys()

To get a list of the values in a dictionary d , use this expression:

d.values()

You can get all the keys and all the values at the same time with this expression, which returns a list of 2-element tuples, in which each tuple has one key and one value as (k, v).

d.items()

Examples:

>>> numberNames
{0: 'zero', 1: 'one', 2: 'two', 5: 'five'}
>>> numberNames.keys()
[0, 1, 2, 5]
>>> numberNames.values()
['zero', 'one', 'two', 'five']
>>> numberNames.items()
[(0, 'zero'), (1, 'one'), (2, 'two'), (5, 'five')]
>>> nameNo
{'forty-leven': 4011, 'two': 2, 'one': 1}
>>> nameNo.keys()
['forty-leven', 'two', 'one']
>>> nameNo.values()
[4011, 2, 1]
>>> nameNo.items()
[('forty-leven', 4011), ('two', 2), ('one', 1)]

Here is another useful method:

d.get(k)

If k is a key in d, this method returns d[k]. However, if k is not a key, the method returns the special value None. The advantage of this method is that if the k is not a key in d, it is not considered an error.

>>> nameNo.get("two")
2
>>> nameNo.get("eleventeen")
>>> huh = nameNo.get("eleventeen")
>>> print huh
None

Note that when you are in conversational mode, and you type an expression that results in the value None, nothing is printed. However, the print statement will display the special value None visually as the example above shows.

There is another way to call the .get() method, with two arguments:

d.get(k, default)

In this form, if key k exists, the corresponding value is returned. However, if k is not a key in d, it returns the default value.

>>> nameNo.get("two", "I have no idea.")
2
>>> nameNo.get("eleventeen", "I have no idea.")
'I have no idea.'

Here is another useful dictionary method. This is similar to the two-argument form of the .get() method, but it goes even further: if the key is not found, it stores a default value in the dictionary.

d.setdefault(k, default)

If key k exists in dictionary d, this expression returns the value d[k]. If k is not a key, it creates a new dictionary entry as if you had said “d[k] = default”.

>>> nameNo.setdefault("two", "Unknown")
2
>>> nameNo["two"]
2
>>> nameNo.setdefault("three", "Unknown")
'Unknown'
>>> nameNo["three"]
'Unknown'

To merge two dictionaries d1 and d2, use this method:

d1.update(d2)

This method adds all the key-value pairs from d2 to d1. For any keys that exist in both dictionaries, the value after this operation will be the value from d2.

>>> colors = { 1: "red", 2: "green", 3: "blue" }
>>> moreColors = { 3: "puce", 4: "taupe", 5: "puce" }
>>> colors.update ( moreColors )
>>> colors
{1: 'red', 2: 'green', 3: 'puce', 4: 'taupe', 5: 'puce'}

Note in the example above that key 3 was in both dictionaries, but after the .update() method call, key 3 is related to the value from moreColors.