Python Forum

Full Version: TypeError: unhashable type: 'set'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all

I have two dictionaries, containing (for example):
dict1 = {'k1':{1,2,3},'k2':{4,5,6},'k3':{7,8,9}}
dict2 = {1:12,2:23,3:34,4:45,5:56,6:67,7:78,8:89,9:90}
I want to find the dict1 set value, having max of dict2 value.
I do:
print(max(list(dict1.values()),key=dict2.get))
and get "TypeError: unhashable type: 'set'"

What am I doing wrong?
I think you are trying to look up the value in dict2 for each of the numbers in dict1 (like 1, 2, 3, 4, etc.)

But the values in dict1 aren't numbers, they're sets of numbers. So your program is trying to find the value of the set itself, and that's not possible.

If you want to unpack the sets, maybe import itertools and then you can
print(max(list(itertools.chain.from_iterable(dict1.values())),key=dict2.get))