Open oldoc63 opened 1 year ago
We can have a list or a dictionary as a value, but we cannot use these data types as keys, or we would get a TypeError.
The word "unhashable" means that this list is an object that can be changed.
Dictionaries in Python rely on each key having a hash value, a specific identifier for the key. If the key can change, that hash value would not be reliable. So the keys must always be unchangeable, hashable data types, like numbers or strings.
A dictionary doesn't have to contain anything. Sometimes we need to create an empty dictionary when we plan to fill it later based on some other input.
To add a single key: value pair
dictionary[key] = value
If we wanted to add multiple key: value pairs to a dictionary at once, we can use the .update() method
If we use a key that already has an entry in the dictionary our value assignment will overwrite the existing value attached to that key.
The keys can be numbers as well. Values can be of any type. We can use a string, a number, a list, or even another dictionary as the value associated with a key.