How could I make a set object hashable? and what does Hashable mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How could I make a set object hashable? and what does Hashable mean?

When I'm trying to convert a list object which is including multiple sets in to a set object, an error occurs. TypeError: unhashable type: 'set' >>> ls=[ {1,2,3}, {'a','s','d'}, {6,7,8} ] >>> set(ls) Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> set(a) TypeError: unhashable type: 'set' how to fix this error? And what is meant by the word "Hashable" in python

24th Feb 2019, 2:14 AM
Kavindu Adhikari
Kavindu Adhikari - avatar
1 Answer
+ 3
Hashable means immutable (kindof). A set cannot contain other sets, only immutable elements, while the set itself is mutable (because you can add/remove elements in a set. Question is, what is your intention? - if you want your set to include the 9 individual values of the original sets, you need to unpack them. Your list contains the 3 sets still holding the elements inside. - if you want nested sets, then the INNER sets must be frozenset https://stackoverflow.com/questions/37105696/how-to-have-a-set-of-sets-in-JUMP_LINK__&&__python__&&__JUMP_LINK?lq=1
24th Feb 2019, 7:05 AM
Tibor Santa
Tibor Santa - avatar