Dictionaries
Dictionaries are incredibly useful containers that allow us to map objects directly to other objects. Dictionaries are extremely efficient at looking up a value, given a specific key object that maps to that value. The secret of the speed is using a hash of the key to locate the value. Every immutable Python object has a numeric hash code; a relatively simple table is used to map the numeric hashes directly to values. This trick means a dictionary never searches the entire collection for a key; the key is transformed to a hash, which locates the associated value (almost) immediately.
Dictionaries can be created either using the dict()
constructor or the {}
syntax shortcut. In practice, the latter format is almost always used. We can prepopulate a dictionary by separating the keys from the values using a colon and separating the key-value pairs using a comma.
We can also create dictionaries using keyword parameters. We can use dict(current=1235...