1

I just found out that d.values() (for some dict d) appears to be hashable not matter the type of the values. However, d.keys() appears to be unhashable, although the elements are guaranteed to be hashable.

Here is an example:

d1 = {'d': 0, 'a': 1, 'e': 2, 'b': 3, 'c': 4}
d2 = {0: [], 1: [[]], 2: [[[]]], 3: [[[[]]]]}

now

print(hash(d1.values()))
print(hash(d2.values()))

Work fine and print some numerical values. But,

print(hash(d1.keys()))

and

print(hash(d2.keys()))

both throw TypeError: unhashable type: 'dict_keys'.

I would have expected the exact opposite! Now I'm curious about the reason why Python does this that way.

cherrywoods
  • 1,284
  • 1
  • 7
  • 18

0 Answers0