unhashable type: ‘dict’ Type Error [duplicate]

dic.values() return list of dict

>>> for key, value in items.items():
...     print dic.values()
...
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
>>>

So, you can’t apply set on dict as dict is not hashable.

Btw you can fix it by:

>>> dic = {}
>>> for key, value in items.items():
...     if not set(value.values()).issubset(set(sum([x.values() for x in dic.values()],[]))):
...         dic[key] = value
...
>>> dic
{1: {'description': u'testing456', 'title': u'testing123'}, 6: {'description':     u'somethingelse', 'title': u'somethingelse'}}
>>>

For python > 3.x

if not set(value.values()).issubset(set(sum([list(x.values()) for x in list(dic.values())],[]))):

Leave a Comment