Extensions > API reference > dict


A language built-in type representing a dictionary (associative mapping). Dictionaries are mutable, indexable, ordered, iterable (equivalent to iterating over its keys), and support fast membership tests of keys using the in operator. The order of the keys is the order of their most recent insertion: it is unaffected by updating the value associated with an existing key, but is affected by removing then reinserting a key.

d = {0: 0, 2: 2, 1: 1}
[k for k in d]  # [0, 2, 1]
d[0], d[2] = "a", "b"
0 in d, "a" in d  # (True, False)
[(k, v) for k, v in d.items()]  # [(0, "a"), (1, 1), (2, "b")]

There are 3 ways to construct a dictionary, each with a different treatment of duplicate keys:

The dict literal expression will result in a dynamic error if duplicate keys are given, regardless of whether the keys are themselves given as literals. The keys' insertion order is the order in which they are given in the expression.

In the dict comprehension, key/value pairs yielded by the generator expression is set in the dictionary in the order yielded: the first occurrence of the key determines its insertion order, and the last determines the value associated to it.

{k: v for k, v in (("a", 0), ("b", 1), ("a", 2))}  # {"a": 2, "b": 1}
{i: 2*i for i in range(3)}  # {0: 0, 1: 2, 2: 4}

The dict() global function is documented elsewhere.


None dict.clear()

Remove all items from the dictionary.


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.


Parameter Description

The key to look for.


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

May return None.


list dict.items()

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


list dict.keys()

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


unknown dict.pop(key, default=unbound)

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


Parameter Description

The key.


a default value if the key is absent.


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.


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.


Parameter Description

The key.


a default value if the key is absent.


None dict.update(args=[], **kwargs)

Update the dictionary with an optional positional argument [pairs] and an optional set of keyword arguments [, name=value[, ...] 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. 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. 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.


Parameter Description

Either a dictionary or a list of entries. Entries must be tuples or lists with exactly two elements: key, value.


Dictionary of additional entries.


list dict.values()

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