+ 8

Python - Dictionaries and keys

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

30th Jun 2020, 2:04 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
3 Réponses
+ 11
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.
30th Jun 2020, 2:09 AM
James Clark I. Vinarao
James Clark I. Vinarao - avatar
+ 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.
2nd Jul 2020, 1:33 AM
madeline
madeline - avatar