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
  • 119,623
  • 25
  • 170
  • 301
Hitopopamus
  • 95
  • 1
  • 8

0 Answers0