Version Added: 2.5
If a subclass of dict defines a method
__missing__, if the key
key is not present, the
d[key]
operation calls that method with
the key
key as argument. The
d[key]
operation then returns or
raises whatever is returned or raised by the
__missing__(key)
call
if the key is not present. No other operations or methods invoke
__missing__. If
__missing__ is not defined,
KeyError is raised.
__missing__ must be a method; it
cannot be an instance variable. For an example, see
collections.defaultdict.
.. describe:: d[key] = value
Set NOTE:
Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary's
history of insertions and deletions. If
items,
keys,
values,
iteritems,
iterkeys, and
itervalues are called with no intervening modifications to the
dictionary, the lists will directly correspond. This allows the
creation of
(value, key)
pairs using
zip: ``pairs =
zip(d.values(), d.keys())``. The same relationship holds for the
iterkeys and
itervalues methods: ``pairs =
zip(d.itervalues(), d.iterkeys())`` provides the same value for
pairs
. Another way to create the same list is ``pairs = [(v, k) for
(k, v) in d.iteritems()]``.