Mar-25-2022, 07:41 AM
Keys need to be immutable for a simple reason. The underlying data structure is called a hash table and key/value pairs are placed into buckets which allows for fast lookup of the key. Which bucket an item ends up in is determined by computing a number that represents the key (its hash value). If you were then to modify a key, there's a chance that lookup would fail because the hash of the new key could mean it should be found in a different bucket. So, the dictionary would need to recompute the hash on a modification, which I assume is not done for reasons of efficiency and complexity.