dict

A language built-in type representating a dictionary (associative mapping). Dictionaries may be constructed with a special literal syntax:
d = {"a": 2, "b": 5}
See also the dict() constructor function. When using the literal syntax, it is an error to have duplicated keys. Use square brackets to access elements:
e = d["a"]   # e == 2
Like lists, they can also be constructed using a comprehension syntax:
d = {i: 2*i for i in range(20)}
e = d[8]       # e == 16
Dictionaries are mutable. You can add new elements or mutate existing ones:
d["key"] = 5

Iterating over a dict is equivalent to iterating over its keys. The in operator tests for membership in the keyset of the dict.

"a" in {"a" : 2, "b" : 5} # evaluates as True
The iteration order for a dict is deterministic and specified as the order in which the keys have been added to the dict. The iteration order is not affected if a value associated with an existing key is updated.

clear

None dict.clear()

Remove all items from the dictionary.

get

unknown dict.get(key, default=None)

Returns 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 throws an error.

Parameters

Parameter Description
key

The key to look for.

default

The default value to use (instead of None) if the key is not found.

May return None.

items

list dict.items()

Returns the list of key-value tuples:
{2: "a", 4: "b", 1: "c"}.items() == [(2, "a"), (4, "b"), (1, "c")]

keys

list dict.keys()

Returns the list of keys:
{2: "a", 4: "b", 1: "c"}.keys() == [2, 4, 1]

pop

unknown dict.pop(key, default=unbound)

Removes a key from the dict, and returns the associated value. If entry with that key was found, return the specified default value;if no default value was specified, fail instead.

Parameters

Parameter Description
key

The key.

default

a default value if the key is absent.

popitem

tuple dict.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() fails. It is deterministic which pair is returned.

setdefault

unknown dict.setdefault(key, default=None)

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.

Parameters

Parameter Description
key

The key.

default

a default value if the key is absent.

update

None dict.update(other)

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

Parameters

Parameter Description
other

dict

The values to add.

values

list dict.values()

Returns the list of values:
{2: "a", 4: "b", 1: "c"}.values() == ["a", "b", "c"]