dict

Return a new dictionary initialized from an optional positional argument or from a set of keyword arguments. If no arguments are given, return a new empty dictionary. If the positional argument arg is a mapping object, return a dictionary mapping the same keys to the same values as does the mapping object. Otherwise the positional argument must be a sequence, a container that supports iteration, or an iterator object. The elements of the argument must each also be of one of those kinds, and each must in turn contain exactly two objects. The first is used as a key in the new dictionary, and the second as the key's value. If a given key is seen more than once, the last value associated with it is retained in the new dictionary.

If keyword arguments are given, the keywords themselves with their associated values are added as items to the dictionary. If a key is specified both in the positional argument and as a keyword argument, the value associated with the keyword is retained in the dictionary. For example, these all return a dictionary equal to {"one": 2, "two": 3}:

* dict(one=2, two=3)

* dict({'one': 2, 'two': 3})

* dict(zip(('one', 'two'), (2, 3)))

* dict([['two', 3], ['one', 2]])

The first example only works for keys that are valid Python identifiers; the others work with any valid keys.
Version Added: 2.2
Version Changed: 2.3 Support for building a dictionary from keyword arguments added.
These are the operations that dictionaries support (and therefore, custom mapping types should support too):

.. describe:: len(d)

Return the number of items in the dictionary d.

.. describe:: d[key]

Return the item of d with key key. Raises a KeyError if key is not in the map.
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 d[key] to value.

.. describe:: del d[key]

Remove d[key] from d. Raises a KeyError if key is not in the map.

.. describe:: key in d

Return True if d has a key key, else False.
Version Added: 2.2
.. describe:: key not in d Equivalent to not key in d.
Version Added: 2.2
clear() Remove all items from the dictionary.

copy()

Return a shallow copy of the dictionary.

fromkeys(seq[, value])

Create a new dictionary with keys from seq and values set to value.

fromkeys is a class method that returns a new dictionary. value defaults to None.
Version Added: 2.3
get(key[, default]) Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

has_key(key)

dict.has_key(key) is equivalent to key in d, but deprecated.

items()

Return a copy of the dictionary's list of (key, value) pairs.
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()]``.
iteritems() Return an iterator over the dictionary's (key, value) pairs. See the note for dict.items.
Version Added: 2.2
iterkeys() Return an iterator over the dictionary's keys. See the note for dict.items.
Version Added: 2.2
itervalues() Return an iterator over the dictionary's values. See the note for dict.items.
Version Added: 2.2
keys() Return a copy of the dictionary's list of keys. See the note for dict.items.

pop(key[, default])

If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.
Version Added: 2.3
popitem() Remove and return an arbitrary (key, value) pair from the dictionary.

popitem is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling popitem raises a KeyError.

setdefault(key[, default])

If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.

update([other])

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.

update accepts either another dictionary object or an iterable of key/value pairs (as a tuple or other iterable of length two). If keyword arguments are specified, the dictionary is then is updated with those key/value pairs: d.update(red=1, blue=2).
Version Changed: 2.4 Allowed the argument to be an iterable of key/value pairs and allowed keyword arguments.
values() Return a copy of the dictionary's list of values. See the note for dict.items.