Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhashable type: 'list' error in python

Tags:

python

list

I have this dictionary:

final = {0: [1, 9], 1: [0, 9], 8: [16, 10], 9: [0, 1], 10: [8, 16], 16: [8, 10]}

And I wanted to convert it to a list, so I used list comprehensions and the result was the following:

myList = [[int(k)]+v for k, v in final.items()]
myList = [[0, 1, 9], [0, 1, 9], [0, 1, 9], [8, 10, 16], [8, 10, 16], [8, 10, 16]]

I also wanted the whole list as well as the elements inside of every small list to be sorted and to erase the duplicates from the list:

for i in myList:
   i.sort()

myList.sort()
list(set(myList))
print(myList)

However, when I run this i get the error "Unhashable type: 'list' ". Is there any other way to implement this? Thank you in advance!

like image 446
kelua Avatar asked Mar 22 '16 20:03

kelua


People also ask

How do I fix Unhashable type list?

This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

What does TypeError Unhashable type list?

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.

How do you fix a TypeError Unhashable type dict?

The Python "TypeError: unhashable type: 'dict'" occurs when we use a dictionary as a key in another dictionary or as an element in a set . To solve the error, use a frozenset instead, or convert the dictionary into a JSON string before using it as a key.

What does Unhashable type set mean in Python?

The Python "TypeError: unhashable type: 'set'" occurs when we use a set as a key in a dictionary or an element in another set . To solve the error, use a frozenset instead, because set objects are mutable and unhashable.


2 Answers

A list is mutable; in Python mutable containers are not hashable. set in turn equires the items to be hashable. You can convert the lists to tuples, which are immutable containers and thus hashable:

>>> myList = [[0, 1, 9], [0, 1, 9], [0, 1, 9], [8, 10, 16], [8, 10, 16], [8, 10, 16]]
>>> list(set(tuple(i) for i in myList))
[(8, 10, 16), (0, 1, 9)]

Note that sets are not sorted so you'd probably want to sort after making the set:

>>> myList = [[0, 1, 9], [0, 1, 9], [0, 1, 9], [8, 10, 16], [8, 10, 16], [8, 10, 16]]
>>> sorted(set(tuple(i) for i in myList))
[(0, 1, 9), (8, 10, 16)]
like image 164

If you are only sorting the sublists to remove dupes you can use frozensets instead and avoid sorting:

final = {0: [1, 9], 1: [0, 9], 8: [16, 10], 9: [0, 1], 10: [8, 16], 16: [8, 10]}

unique = list(map(list, {frozenset([k] + v) for k, v in final.items()}))

Which will give you:

[[0, 1, 9], [16, 8, 10]]

You could still sort the remaining sublists which would still be faster than sorting them all first and then removing especially if the sublists are large and/or you have a lot of dupes.

unique = list(map(sorted, {frozenset([k] + v) for k, v in final.items()}))

print(unique)

Which will give you ordered output if necessary:

[[0, 1, 9], [8, 10, 16]]
like image 33
Padraic Cunningham Avatar answered Nov 14 '22 23:11

Padraic Cunningham