Built-in class dict
Purpose
- Support sets of mappings from one value to another (think dictionary).
- Suppose below that
d
is adict
.
Literals
- The empty dictionary literal is
{}
.
Actions
len(d)
returns the number of mappings ind
.
d.get(k)
returns the value that keyk
maps to ind
. If there is no such value, returnsNone
.
d[k]
returns the value that keyk
maps to ind
. If there is no such value, an error occurs.
d[k] = v
setsd
to have a mapping fromk
tov
. If there was a prior mapping fromk
ind
, then that mapping is removed. In dictionary parlance, there is a now a mapping from keyk
to valuev
ind
.
del d[k]
removesk
’s mapping ind
.
(k in d)
indicates whetherk
is a key value ind
.
(k not in d)
indicates whetherk
is not a key value ind
.
d.clear()
removes all mappings ind
.
d.keys()
are the keys ford
. There are no duplicates among the keys.
d.values()
are the values of the keys ind
. There are duplicate values if more than one key maps to the same value.
d.pop(k)
removes k’s mapping in d and returns the value of that mapping.
d.popitem()
removes and returns an arbitrary mapping fromd
.