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
d.has_key(k)
This is the equivalent of the expression “”: it returns k in dTrue 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
d.keys()
To get a list of the values in a dictionary , use this
expression:
d
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 is a key
in k, this
method returns d. However, if
d[k] is not a
key, the method returns the special value kNone. The advantage of this method is that if
the is not
a key in k,
it is not considered an error.
d
>>> 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 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.
d.setdefault(k,default)
If key
exists in dictionary k, this expression returns the value d. If d[k] is not a key, it creates a new
dictionary entry as if you had said “k”.
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
d1.update(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.