Comparing 2 codes in python dictionaries | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Comparing 2 codes in python dictionaries

In this code we receive ERROR because dictionaries logic says keys should be immutable I thought in this code we should receive ERROR with same reason As a key in dictionaries 1 in first code has difference to 1 in second code ? exactly why ?

29th Jan 2018, 10:27 PM
NIMA - avatar
1 Answer
The error is not about keys having to be immutable. It's about the key being not hashable. Most objects define a hash function, which returns a unique value for each object. That hash function is used for keys of a dictionary, which is essentially a hashtable. A list doesn't support hashing. The list elements can be hashed, but not the list itself. That's why you can't use a list as the key.
29th Jan 2018, 10:36 PM
SplittyDev - avatar