dict
type
dict.clear
def dict.clear() -> None
dict.clear: clear a dictionary
D.clear()
removes all the entries of dictionary D and returns None
.
It fails if the dictionary is frozen or if there are active iterators.
x = {"one": 1, "two": 2}
x.clear()
x == {}
dict.get
def dict.get(key, default = ..., /)
dict.get: return an element from the dictionary.
D.get(key[, default])
returns the dictionary value corresponding to
the given key. If the dictionary contains no such value, get
returns None
, or the value of the optional default
parameter if
present.
get
fails if key
is unhashable.
x = {"one": 1, "two": 2}
x.get("one") == 1
x.get("three") == None
x.get("three", 0) == 0
dict.items
def dict.items() -> list[(typing.Any, typing.Any)]
dict.items: get list of (key, value) pairs.
D.items()
returns a new list of key/value pairs, one per element in
dictionary D, in the same order as they would be returned by a for
loop.
x = {"one": 1, "two": 2}
x.items() == [("one", 1), ("two", 2)]
dict.keys
def dict.keys() -> list
dict.keys: get the list of keys of the dictionary.
D.keys()
returns a new list containing the keys of dictionary D, in
the same order as they would be returned by a for
loop.
x = {"one": 1, "two": 2}
x.keys() == ["one", "two"]
dict.pop
def dict.pop(key, default = ..., /)
dict.pop: return an element and remove it from a dictionary.
D.pop(key[, default])
returns the value corresponding to the specified
key, and removes it from the dictionary. If the dictionary contains no
such value, and the optional default
parameter is present, pop
returns that value; otherwise, it fails.
pop
fails if key
is unhashable, or the dictionary is frozen or has
active iterators.
x = {"one": 1, "two": 2}
x.pop("one") == 1
x == {"two": 2}
x.pop("three", 0) == 0
x.pop("three", None) == None
Failure:
{'one': 1}.pop('four') # error: not found
dict.popitem
def dict.popitem() -> (typing.Any, typing.Any)
dict.popitem: returns and removes the first key/value pair of a dictionary.
D.popitem()
returns the first key/value pair, removing it from the
dictionary.
popitem
fails if the dictionary is empty, frozen, or has active
iterators.
x = {"one": 1, "two": 2}
x.popitem() == ("one", 1)
x.popitem() == ("two", 2)
x == {}
Failure:
{}.popitem() # error: empty dict
dict.setdefault
def dict.setdefault(key, default = ..., /)
dict.setdefault: get a value from a dictionary, setting it to a new value if not present.
D.setdefault(key[, default])
returns the dictionary value
corresponding to the given key. If the dictionary contains no such
value, setdefault
, like get
, returns None
or the value of the
optional default
parameter if present; setdefault
additionally
inserts the new key/value entry into the dictionary.
setdefault
fails if the key is unhashable or if the dictionary is
frozen.
x = {"one": 1, "two": 2}
x.setdefault("one") == 1
x.setdefault("three", 0) == 0
x == {"one": 1, "two": 2, "three": 0}
x.setdefault("four") == None
x == {"one": 1, "two": 2, "three": 0, "four": None}
dict.update
def dict.update(
pairs: typing.Iterable[(typing.Any, typing.Any)] | dict = ...,
/,
**kwargs,
) -> None
dict.update: update values in the dictionary.
D.update([pairs][, name=value[, ...])
makes a sequence of key/value
insertions into dictionary D, then returns None.
If the positional argument pairs
is present, it must be None
,
another dict
, or some other iterable.
If it is another dict
, then its key/value pairs are inserted into D.
If it is an iterable, it must provide a sequence of pairs (or other
iterables of length 2), each of which is treated as a key/value pair
to be inserted into D.
For each name=value
argument present, the name is converted to a
string and used as the key for an insertion into D, with its
corresponding value being value
.
update
fails if the dictionary is frozen.
x = {}
x.update([("a", 1), ("b", 2)], c=3)
x.update({"d": 4})
x.update(e=5)
x == {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
dict.values
def dict.values() -> list
dict.values: get the list of values of the dictionary.
D.values()
returns a new list containing the dictionary's values, in
the same order as they would be returned by a for
loop over the
dictionary.
x = {"one": 1, "two": 2}
x.values() == [1, 2]