Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why dict objects are unhashable in python?

I mean why cant we put key of dict as dict?

that means we can't have dictionary having key as another dictionary...

like image 446
shahjapan Avatar asked Dec 24 '09 08:12

shahjapan


People also ask

What does Unhashable type dict mean in Python?

The “TypeError: unhashable type: 'dict'” error is raised when you try to create an item in a dictionary whose key is an unhashable object. Only immutable objects like strings, tuples, and integers can be used as a key in a dictionary.

How do you resolve Unhashable type dictionary?

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.

Why is set Unhashable 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.

Why are dictionaries hashable?

When we use a key that contains an unhashable type, i.e. a list, the underlying hash map cannot guarantee the key will map to the same bucket every single time. If we can't hash our key, we can't use it in our dictionary. Therefore, Python dictionaries require hashable dict keys. Having immutable keys is not enough.


1 Answers

Short answer: because they are mutable containers.

If a dict was hashed, its hash would change as you changed its contents.

like image 55
Ben James Avatar answered Sep 20 '22 16:09

Ben James