Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary : TypeError: unhashable type: 'list'

I'm having troubles in populating a python dictionary starting from another dictionary.

Let's assume that the "source" dictionary has string as keys and has a list of custom objects per value.

I'm creating my target dictionary exactly as I have been creating my "source" dictionary how is it possible this is not working ?

I get

TypeError: unhashable type: 'list' 

Code :

aTargetDictionary = {} for aKey in aSourceDictionary:     aTargetDictionary[aKey] = []     aTargetDictionary[aKey].extend(aSourceDictionary[aKey]) 

The error is on this line : aTargetDictionary[aKey] = []

like image 943
codeJack Avatar asked Dec 16 '11 09:12

codeJack


People also ask

How do I fix TypeError Unhashable type list in Python?

The “TypeError: unhashable type: 'list'” error is raised when you try to assign a list as a key in a dictionary. To solve this error, ensure you only assign a hashable object, such as a string or a tuple, as a key for a dictionary.

How do I fix Unhashable type error?

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 is 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.

Is list hashable in Python?

All of Python's immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are.


2 Answers

The error you gave is due to the fact that in python, dictionary keys must be immutable types (if key can change, there will be problems), and list is a mutable type.

Your error says that you try to use a list as dictionary key, you'll have to change your list into tuples if you want to put them as keys in your dictionary.

According to the python doc :

The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity, the reason being that the efficient implementation of dictionaries requires a key’s hash value to remain constant

like image 170
Cédric Julien Avatar answered Nov 06 '22 14:11

Cédric Julien


This is indeed rather odd.

If aSourceDictionary were a dictionary, I don't believe it is possible for your code to fail in the manner you describe.

This leads to two hypotheses:

  1. The code you're actually running is not identical to the code in your question (perhaps an earlier or later version?)

  2. aSourceDictionary is in fact not a dictionary, but is some other structure (for example, a list).

like image 36
NPE Avatar answered Nov 06 '22 15:11

NPE