dictionary

A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.

In more detail

Dictionaries are hash tables, so looking up, adding and deleting by key take constant time on average. Keys must be hashable, and since Python 3.7 a dict remembers the order in which keys were inserted. Reading a missing key with d[key] raises KeyError, while d.get(key) returns None.

Examples

users = {'alice': 1, 'bob': 2}; 'bob' in users  # a hash lookup, not a scan
{'amount': 100}
The fold lessons use a dictionary as the simplest state store.
spanish = {"hello": "hola", "red": "rojo"}
print(spanish["red"])
In this course: Looking up a missing key raises KeyError. When pytest compares two dictionaries that differ, it names the differing keys and hides the identical ones.

Related terms

Taught in

References

Certification courses