Python Forum
TypeError: unhashable type: 'set'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: unhashable type: 'set'
#1
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?
Reply
#2
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 2,352 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  Unhashable error - Histogram code lsbpython 1 1,584 Aug-07-2022, 04:02 PM
Last Post: Yoriz
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 11,317 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 3,599 May-07-2022, 08:40 AM
Last Post: ibreeden
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,896 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 8,715 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  TypeError: 'type' object is not subscriptable Stef 1 6,587 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,840 May-17-2020, 05:56 PM
Last Post: shockwave
  calculate_bai -- TypeError: unsupported operand type(s) for *: 'float' and 'NoneType' pantherd 1 3,894 Apr-21-2020, 12:31 PM
Last Post: anbu23
  TypeError: can't multiply sequence by non-int of type 'float' DimosG 3 4,545 Apr-04-2020, 05:16 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020