Suppose I have this dictionary:
items = {1: {'title': u'testing123', 'description': u'testing456'},
2: {'description': u'testing123', 'description': u'testing456'},
3: {'description': u'testing123', 'description': u'testing456'},
4: {'description': u'testing123', 'description': u'testing456'},
5: {'description': u'testing123', 'description': u'testing456'},
6: {'description': u'somethingelse', 'description': u'somethingelse'}}
I want to filter out the duplicate values, so that in the end I'd get
{1: {'title': u'testing123', 'description': u'testing456'}, 6: {'title': u'something', 'description': u'somethingelse'}}
I wrote this code:
dic = {}
for key, value in items.items():
if not set(value.values()).issubset(set(dic.values())):
dic[key] = value
however I get the error message TypeError: unhashable type: 'dict'
. I am not sure why this happens and how to fix it.
This is inspired by another question and my failed attempt to solve it.
dic.values() return list of dict
So, you can't apply set on dict as dict is not hashable.
Btw you can fix it by:
For python > 3.x
You are trying to create a set of dicts, but that's not possible, since dicts are unhashable (because they are mutable -- whether they are equal can change as you modify/add/remove pairs in the dict).
Perhaps instead of using the dicts, you can use tuples of their values for your set, a la
if not set((v['description_a'], v['description_b]) for v in value.values()).issubset((v['description_a'], v['description_b]) for v in set(dic.values())):
or similar?Edit: If you must use a set, as others have noted you have to use a hashable object like a tuple:
gives for
unique_items
:If
items
is not huge (or, at least if the output dictionary isn't expected to be huge):(assuming the first key in your dictionary example should have been
title
). But you can't predict what the keys to this dictionary will be if duplicates do exist initems
.