I mean why cant we put key of dict as dict?
that means we can't have dictionary having key as another dictionary...
I mean why cant we put key of dict as dict?
that means we can't have dictionary having key as another dictionary...
Short answer: because they are mutable containers.
If a dict was hashed, its hash would change as you changed its contents.
None of the mutable container types in Python are hashable, because they are mutable and thus their hash value can change over their lifetime.
As others have said, the hash value of a dict changes as the contents change.
However if you really need to use dicts as keys, you can subclass dict to make a hashable version.
>>> class hashabledict(dict):
... def __hash__(self):
... return id(self)
...
>>> hd = hashabledict()
>>> d = dict()
>>> d[hd] = "foo"
>>> d
{{}: 'foo'}
>>> hd["hello"] = "world"
>>> d
{{'hello': 'world'}: 'foo'}
This replaces the hash value used for the dict with the object's address in memory.