A Python dictionary (dict) is a mapping from keys to values. It’s one of Python’s built-in container types, alongside lists, tuples, and sets, and it’s the data structure that shows up most often when we want to retrieve a value by a name rather than by a numeric index.
my_dict = {"name": "Alice", "age": 31, "email": "alice@example.com"}We retrieve a value by indexing with its key:
print(my_dict["name"]) # AliceWe list keys and values with .keys() and .values(). We delete an entry with del my_dict["age"]. And we iterate over both together with .items():
for key, value in my_dict.items():
print(f"{key}: {value}")The last line uses an f-string — a string with a leading f whose contents inside curly braces are evaluated as Python expressions and inserted into the result. F-strings are by far the cleanest way to format strings in modern Python.
Dictionaries are why h5py’s interface to HDF5 feels natural — an open HDF5 file behaves like a dictionary whose keys are group and dataset names. A Pandas DataFrame also looks dict-like when accessed by column name. Dictionaries also appear at the heart of JSON, since a JSON object is literally a key-value mapping that deserializes into a Python dict.
Under the hood, Python dictionaries are hash tables — average lookup, insertion, and deletion. Since Python 3.7, iteration order is guaranteed to match insertion order, which lets dicts replace collections.OrderedDict for most purposes.