September 2026
1051 pages
35h 4m
English
The fifth and last data type we want to introduce at this point is the dictionary. A dictionary stores mappings of keys to values. To create a dictionary, the key-value pairs are written in curly brackets separated by commas. There’s a colon between a key and the corresponding value:
>>> d = {"key1": "value1", "key2": "value2"}
You can access an underlying value via a key. For this purpose, square brackets are used as in lists:
>>> d["key1"]'value1'>>> d["key2"]'value2'
You can also use this access operation to modify values or enter new key-value pairs into the dictionary:
>>> d["key2"] = "value2.1">>> d["key2"]'value2.1'>>> d["key3"] = "value3">>> d["key3"]'value3'>>> d{'key1': 'value1', 'key2': 'value2.1', 'key3':
Read now
Unlock full access