Python - Dictionaries and keys | SoloLearn: Learn to code for FREE!

+7

Python - Dictionaries and keys

Why in a dictionary, key has to be an immutable?

6/30/2020 2:04:51 AM

Paolo De Nictolis

3 Answers

New Answer

+10

Because if a key is mutable its value could change. http://effbot.org/pyfaq/why-must-dictionary-keys-be-immutable.htm Thanks for your question and happy coding.

+8

Paolo De Nictolis Official Answer by Python 👇 https://docs.python.org/2/faq/design.html#why-must-dictionary-keys-be-immutable

+1

python hashes the dictionary keys for sorting purposes. if you could change the keys the hashing algorithm could clash. i believe you can make lists of lists if you wanted to in python to get around this, but that would make working with your data slower. and theres more complex ways to get around it listed in the answers above.