Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unhashable type: 'OrderedDict'

I am not at all unfamiliar with the concept of:

TypeError: unhashable type: 'OrderedDict'

But I can not understand how the following line of codes can produce such a stack-trace.

89:     @staticmethod
90:     def diff(var1, var2, path=[], level=0, curpath=[]):
...
101:        elif isinstance(var1, list) and isinstance(var2, list):
102:            l1s = set(var1)
103:            l2s = set(var2)
104:            retlist = []

  File "myFile.py", line 102, in diff
    l1s = set(var1)
TypeError: unhashable type: 'OrderedDict'

How can line 102, in the above code throw such an exception?

like image 637
theAlse Avatar asked Apr 08 '13 13:04

theAlse


People also ask

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 does Unhashable type mean in Python?

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 I fix Unhashable type NumPy Ndarray?

Adding a NumPy Array to a Set We are attempting to add a ndarray to a set in this case. Therefore, the code above should return an error: We can solve this by adding each array element instead of the array object into the set. This should add all the elements of the array to the set.

What is Unhashable type set?

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. Here are 2 examples of how the error occurs.


2 Answers

Some data structures (most notably dicts and sets) require objects they contain (keys in the case of dictionaries, items in the case of sets) to implement the __hash__() magic method, so that calling hash(obj) returns a value.

This is required to optimize the structure, and together with immutability to help guarantee uniqueness of contained objects.

In your case, var1 contains some object that is not hashable (it does not implement hash()). This object is an OrderedDict, which is a mutable object and is not hashable by design.

As an example of an other object type which is mutable and not hashable by design, consider list and this example:

>>> L = [1, 2, 3]
>>> set([L])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> hash(L)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

If you're using set() to ensure uniqueness then you must go some other way, though it's not clear from your question.

like image 197
Davide R. Avatar answered Nov 04 '22 12:11

Davide R.


dict(including OrderedDict) in python are mutable containers.

If a dict was hashed, its hash value would be changed as long as you changed the dict's contents.

like image 28
Yarkee Avatar answered Nov 04 '22 11:11

Yarkee