A number of useful methods are defined on any Python
dictionary. To test whether a key

exists in a dictionary `k`

, use this method:
`d`

.has_key(`d`

)`k`

This is the equivalent of the expression “

”: it returns * k* in

`d`

`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

, use this
expression:
`d`

.keys()`d`

To get a list of the values in a dictionary

, use this
expression:
`d`

.values()`d`

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`

.items()`d`

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:

.get(`d`

)`k`

If

is a key
in `k`

, this
method returns `d`

. However, if
* d*[

`k`

`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:

.get(`d`

,`k`

)`default`

In this form, if key

exists, the corresponding value is returned.
However, if `k`

is not a key in `k`

, it returns the `d`

value.
`default`

>>> 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.

.setdefault(`d`

,`k`

)`default`

If key

exists in dictionary `k`

, this expression returns the value `d`

. If * d*[

`k`

`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

and `d1`

, use this method:
`d2`

.update(`d1`

)`d2`

This method adds all the key-value pairs from

to `d2`

. For any keys
that exist in both dictionaries, the value after this
operation will be the value from `d1`

.
`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`

.