Elementary introduction to Python dictionaries
Every programming language has a characteristic data structure. For Python, it is the dictionary.

Photo by Mitchell Luo on Unsplash
Python dictionaries are one of the most fundamental data structures in the language. Probably the most fundamental one.
Other names for a dictionary are a hash table or a key-value-store. They all work essentially the same way. As the name implies, you use a “key” to store and look up a “value.” This operates kind of like a file on a computer’s filesystem: you use the file name to find the file and get its contents.
d[k1] = v1 # the key is k1 and the value is v1
One of the important properties of the key is that it must be hashable. (This means it can be the argument of a “hash function” - a rapid mapping function - to generate a unique value quickly.) What that means in practice is that once the value is created it doesn’t change (this is also called “immutable”, or unchangeable). In Python, strings (text), numbers, and tuples are immutable and can be used as the keys of a dictionary. However, lists, dictionaries, and other mutable data structures are not hashable. An interesting exception comes from user-defined classes: instances of user defined classes are mutable, but they have a unique identifier that is fixed. Therefore, they are treated as hashable. Yes, it gets complicated.
The value stored in a dictionary doesn’t have the same constraint. You can store any kind of data type as a value in a dictionary.
The versatility and flexibility of dictionaries means that Python programmers use them a lot. Sometimes too much. One of the key fruits of experience is to learn which data structure to use when. It is a subtle understanding that grows through experience. There is always more to learn.
Next steps

Photo by Ales Nesetril on Unsplash
Originally published on Substack.