-1

I'd like to reverse my key and value in a dict. Item numbers are created as QTreeWidgetItem, and dataBlock#s are the data object for the item number.

my_dict = { dataBlock1:item1, dataBlock2:item2, dataBlock3:item3}

inv_dict = {v:k for k,v in my_dict.items()}

I got the error:

TypeError: unhashable type: 'QTreeWidgetItem'

How do I reverse the key & value in this case. I need item numbers as keys in the inv_dict for another use purpose.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
Bebe Win
  • 1
  • 1

1 Answers1

0

Since items are unhashable, one solution is that store each item in a list and use its index as the value in my_dict. Now you have integers as values and you can reverse the key values. Another way that I'm not sure about that, you can implement __hash__ function for your item's class.

Nima
  • 11
  • 5