1

I am trying this simple function that switches the keys and values for a given dictionary:

def reverse_dict(d):
    return {y:x for x,y in d.items()}

Works fine if the values are immutable, however if one of the values is a list I get the following error:

TypeError: unhashable type: 'list'

Why does the value have to be immutable for this function to work? I'm interested in what happens in terms of python's memory model.

martineau
  • 112,593
  • 23
  • 157
  • 280
Hitopopamus
  • 95
  • 1
  • 7

0 Answers0