These operations are available on any dictionary object :
D
len(D)
Returns the number of key-value pairs in .
D
D[k]
If dictionary has a key whose value is equal to D, this operation
returns the corresponding value for that key. If there
is no matching key, it raises a kKeyError
exception.
>>> signals = {0: 'red', 1: 'yellow', 2: 'green'}
>>> signals[2]
'green'
>>> signals[88]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 88
D[k] = v
If dictionary does not have a key-value pair whose key
equals D, a
new pair is added with key k and value k.
v
If already
has a key-value pair whose key equals D, the value of
that pair is replaced by k.
v
k in D
A predicate that
tests whether has a key equal to D.
k
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> 3 in roster
True
>>> 88 in roster
False
k not in D
A predicate that
tests whether does not have a key
equal to D.
k
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> 3 not in roster
False
>>> 88 not in roster
True
del D[k]
In Python, del is a statement, not a
function; see Section 22.3, “The del statement: Delete a name or part
of a value”.
If dictionary has a key-value pair whose key equals D, that key-value
pair is deleted from k. If there is no matching key-value pair, the
statement will raise a DKeyError
exception.
>>> rgb = {'red':'#ff0000', 'green':'#00ff00', 'blue':'#0000ff'}
>>> del rgb['red']
>>> rgb
{'blue': '#0000ff', 'green': '#00ff00'}
>>> del rgb['cerise']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'cerise'
D.get(k, x)
If dictionary has a key equal to D, it returns the corresponding
value, that is, it is the same as the expression
“x”.
D[x]
However, if has no key-value pair for key D, this method returns the
default value k. The second argument is optional; if omitted,
and x has no
key equal to D, it returns kNone.
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> roster.get(2)
'Ray'
>>> v = roster.get(8)
>>> print v
None
>>> roster.get(2, 'Not found')
'Ray'
>>> roster.get(8, 'Not found')
'Not found'
D.has_key(k)
A predicate that
returns True if has a key D.
k
>>> signals = {0: 'red', 1: 'yellow', 2: 'green'}
>>> signals.has_key(1)
True
>>> signals.has_key(88)
False
D.items()
Returns the contents of dictionary as a list of two-element
tuples D(, in no
particular order.
k,
v)
>>> signals = {0: 'red', 1: 'yellow', 2: 'green'}
>>> signals.items()
[(0, 'red'), (1, 'yellow'), (2, 'green')]
D.iteritems()
Returns an iterator that generates the values from
dictionary
as a sequence of two-element tuples D(. See Section 24.2, “Iterators: Values that can produce a sequence of
values”.
k, v)
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> rosterScan = roster.iteritems()
>>> for n, name in rosterScan:
... print "{0:04d}: {1}".format(n, name)
...
0001: Pat
0002: Ray
0003: Min
D.iterkeys()
Returns an iterator that generates the keys from
dictionary . See Section 24.2, “Iterators: Values that can produce a sequence of
values”.
D
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> nScan = roster.iterkeys()
>>> for n in nScan:
... print n,
...
1 2 3
D.itervalues()
Returns an iterator that generates the values from
dictionary . See Section 24.2, “Iterators: Values that can produce a sequence of
values”.
D
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> nameScan = roster.itervalues()
>>> for name in nameScan:
... print name,
...
Pat Ray Min
D.keys()
Returns a list of the key values in dictionary , in no
particular order.
D
>>> signals = {0: 'red', 1: 'yellow', 2: 'green'}
>>> signals.keys()
[1, 0, 2]
D.popitem()
Returns an arbitrary entry from dictionary as a (D tuple, and also removes
that entry. If key, value) is empty, raises a DKeyError
exception.
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> roster.popitem()
(1, 'Pat')
>>> roster
{2: 'Ray', 3: 'Min'}
D.setdefault(k, x)
If dictionary has a key equal to D, this method returns the
corresponding value k.
D[k]
If has no
key equal to D, the method returns the default value k. However,
unlike the x method, it also creates
a new key-value pair .get()( in k, x).
D
As with the method, the second argument is optional, and
defaults to the value .get()None.
D.values()
Returns a list of the values from key-value pairs in
dictionary , in no particular order. However, if you call both
the D.items() and .values()
methods of a dictionary without changing that
dictionary's contents between those calls, Python
guarantees that the ordering of the two results will be
the same.
>>> signals = {0: 'red', 1: 'yellow', 2: 'green'}
>>> signals.values()
['yellow', 'red', 'green']
>>> signals.keys()
[1, 0, 2]
D.update(D2)
Merge the contents of dictionary into
dictionary D2. For any key-value pairs that have the same key in
both D and
D, the value for that key in
D2 after this
operation will be the value from D, not
the value from D2.
D
>>> roster={1:'Pat', 2:'Ray', 3:'Min'}
>>> newer={3:'Bev', 4:'Wes'}
>>> roster.update(newer)
>>> roster
{1: 'Pat', 4: 'Wes', 2: 'Ray', 3: 'Bev'}
>>> newer
{3: 'Bev', 4: 'Wes'}