Why it is true false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why it is true false?

def ishashtable(x): try set={x} return True except TypeError: return False print(ishashable(2), ishashable([2])) True False it is true because set={x} in our case 2 is possible/true but why is false for [2] ?

19th Nov 2019, 10:55 PM
THEGreatGatsby
THEGreatGatsby - avatar
2 Answers
+ 2
(2) and {2}are both of same type, tuple, whereas [2] is a list.
19th Nov 2019, 11:09 PM
Helena
Helena - avatar
+ 1
1. Please write the code in code playground next time, because it is easier to read and understand. Or at least, if you're posting it as plain text, write it with the appropriate identation because this looks messy. 2. Use google for these kind of questions first before posting in here, because there were some threads opened in stackoverflow and similar that discussed this exact same problem, so please make an effort before you post such things. Mutable data types like lists and dictionaries are unhashable. The only hashable data types are the immutable ones, like strings, integers...
19th Nov 2019, 11:05 PM
Aymane Boukrouh
Aymane Boukrouh - avatar