0

I have already checked some question-answers related to Unhashable type : 'list' in stackoverflow, but none of them helped me. Here is my partial code:

keyvalue = {};
input_list_new = input_list;

for file in os.listdir('Directory/'):
    pathname = os.path.join('Directory/', file)
    dict_table = []; # creates blank 2d array
    with open(pathname) as dict_file:
        for line in dict_file:
            dict_table.append(line.strip().split("\t"))
    dict_list = [];
    for i in list(range(0, len(dict_table))):
        dict_list.append(dict_table[i][0])
    matched = list(set(dict_list) & set(input_list)) 
    for i in list(range(0, len(matched))):
        temp = [dict_table[0][0]] + dict_table[(dict_list.index(matched[i]))]

        input_list_new.insert((input_list_new.index(matched[i])), temp)

        dict = {matched[i] : temp}
        keyvalue.update(dict)

where dict_table is a list of lists, dict_list is just a list & keyvalue is a python dictionary. The entire code runs well when ever I comment the line input_list_new.insert((input_list_new.index(matched[i])), temp), but without that line being commented, it shows Unhashable type : 'list' error.

7
  • I'd suggest eliminating the semi-colons; this is Python :) Commented Jan 18, 2016 at 6:38
  • can you show the error exactly ? in which line is the error coming ?> Commented Jan 18, 2016 at 6:41
  • Please paste the full Traceback of the error into your question; put it in a code block to preserve formatting. BTW, it's a bad idea to use dict as a variable name as that shadows the built-in dict type, which can lead to mysterious bugs if you attempt to create a dictionary using dict(). That's not a problem for the partial code you've posted, but it may affect subsequent code in that function / module. Commented Jan 18, 2016 at 6:54
  • Use pieces from the problem line to see which of those is causing the problem. That is, put matched[i] on its on line, put input...() on its own, etc Commented Jan 18, 2016 at 7:00
  • Some place or other you are using a list as a key to a dictionary or set, e.g. ` set([[1,2],3])` produces this error. So does {}[[1,2]]. Commented Jan 18, 2016 at 7:02

2 Answers 2

1

The error message does not correspond to the line

input_list_new.insert((input_list_new.index(matched[i])), temp)

being commented out. If anything, the culprit is the line

dict = {matched[i] : temp}

The problem should be that you are trying to use a list as your dictionary key. Here is the problem in a simple reproducible way:

{[]: 5}  # throws TypeError: unhashable type: 'list'

The reason it fails, is because a dictionary is also be called a hashmap (in other languages). The key must be hashable, and in Python trying to use a list as the key throws your error becauses lists are not hashable (see "hashable" entry here).

Avoid using a list as your dictionary key will fix the problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey Martin, thanks for your suggestion. But I'm not using a list as a key of the python dictionary, matched[i] is just a string.
is it possible that a list is somehow inserted into matched? Can you do print(matched[i]) each time before dict = {matched[i] : temp} and see the last value before it crashes? Also it would be very useful to see what line number the error happens on.
A dictionary is not necessarily a hashmap so the terms aren't synonymous.
1

Here's my guess .... You mentioned that the error is with the line matched = list(set(dict_list) & set(input_list)) .. That is probably because in either input_list or dict_list, you have a list within a list .... the items in a set need to hashable, and hence immutable .. for example you cannot do set([1,5,6,[1,3]]) ... that will give the unhashable type list error ... but you can do set([1,5,6,(1,3)]) because a tuple is immutable and hashable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.