class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.
If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an iterable object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.
If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument.
To illustrate, the following examples all return a dictionary equal to {"one": 1, "two": 2, "three": 3}
:
>>> a = dict(one=1, two=2, three=3) >>> b = {'one': 1, 'two': 2, 'three': 3} >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) >>> e = dict({'three': 3, 'one': 1, 'two': 2}) >>> a == b == c == d == e True
Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used.
These are the operations that dictionaries support (and therefore, custom mapping types should support too):
-
len(d)
-
Return the number of items in the dictionary d.
-
d[key]
-
Return the item of d with key key. Raises a
KeyError
if key is not in the map.If a subclass of dict defines a method
__missing__()
and key is not present, thed[key]
operation calls that method with the key key as argument. Thed[key]
operation then returns or raises whatever is returned or raised by the__missing__(key)
call. 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:>>> class Counter(dict): ... def __missing__(self, key): ... return 0 >>> c = Counter() >>> c['red'] 0 >>> c['red'] += 1 >>> c['red'] 1
The example above shows part of the implementation of
collections.Counter
. A different__missing__
method is used bycollections.defaultdict
.
-
d[key] = value
-
Set
d[key]
to value.
-
del d[key]
-
Remove
d[key]
from d. Raises aKeyError
if key is not in the map.
-
key in d
-
Return
True
if d has a key key, elseFalse
.
-
key not in d
-
Equivalent to
not key in d
.
-
iter(d)
-
Return an iterator over the keys of the dictionary. This is a shortcut for
iter(d.keys())
.
-
clear()
-
Remove all items from the dictionary.
-
copy()
-
Return a shallow copy of the dictionary.
-
classmethod 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 toNone
.
-
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 aKeyError
.
-
items()
-
Return a new view of the dictionary’s items (
(key, value)
pairs). See the documentation of view objects.
-
keys()
-
Return a new view of the dictionary’s keys. See the documentation of view objects.
-
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.
-
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, callingpopitem()
raises aKeyError
.
-
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 tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs:d.update(red=1, blue=2)
.
-
values()
-
Return a new view of the dictionary’s values. See the documentation of view objects.
Dictionaries compare equal if and only if they have the same (key,
value)
pairs. Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise TypeError
.
Please login to continue.