PyDocs

Nav

Python Runtime Services

dict

builtins

class · Added in 3.8
  • dict(**kwargs)
  • dict(mapping, /, **kwargs)
  • dict(iterable, /, **kwargs)
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
    (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
    d = {}
    for k, v in iterable:
        d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
    in the keyword argument list.  For example:  dict(one=1, two=2)
Changed in 3.7, 3.8
  • 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6.
  • 3.8: Dictionaries are now reversible.
Members of dict

c

clear
Remove all items from the dict.
copy
Return a shallow copy of the dict.

f

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

g

get
Return the value for key if key is in the dictionary, else default.

i

items
Return a set-like object providing a view on the dict's items.

k

keys
Return a set-like object providing a view on the dict's keys.

p

pop
If the key is not found, return the default if given; otherwise, raise a KeyError.
popitem
Remove and return a (key, value) pair as a 2-tuple.

s

setdefault
Insert key with a value of default if key is not in the dictionary.

u

update
If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by:…

v

values
Return an object providing a view on the dict's values.
1 Example
>>> 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})
>>> f = dict({'one': 1, 'three': 3}, two=2)
>>> a == b == c == d == e == f
True
from the Python docs
See Also
No see-alsos for builtins/dict
0 Notes
No notes for dict