Skip to content Skip to sidebar Skip to footer

Unhashable Type List Python

When i run my program (anagram solver) i get error Unhashable type: list, thats when i turned wordList into a tuple but i still get the error. The word.txt contains a bunch of wor

Solution 1:

You can just turn the list back into a string (str)

sortWord = ''.join(sorted(word))

other suitable choices could be tuple or frozenset

This next line is a bug though - it's just the set of letters

setWord = set(word)

You'll want to make the value a set containing the actual words.

eg.

sortWord = ''.join(sorted(word))
if sortWord not in sortDict:
    sortDict[sortWord] = set()
sortDict[sortWord].add(setWord)

Solution 2:

sorted inbuilt function in python will return you list.

Your line of code

sortWord = sorted(word)

will give you sortWord as list. You have to convert it to python hashable object.


Post a Comment for "Unhashable Type List Python"