+ 1

Comparing 2 codes in python dictionaries

In this code we receive ERROR because dictionaries logic says keys should be immutable https://code.sololearn.com/c73rDnjoI6VC/?ref=app I thought in this code we should receive ERROR with same reason https://code.sololearn.com/cMjC1Q1Bqkgy/?ref=app 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
NIMA - avatar
1 Answer
0
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
SplittyDev - avatar