You have your loops inverted; you need to loop over shared_list
*first:
[dict(tupleized) for item in shared_list for tupleized in set(tuple(item.items()))]
A list comprehension lists the loops in nesting order; left is outermost.
Next problem is that your values contain lists, these cannot be added to a set unaltered.
Next is that you need to use the set externally to the loop to test if a dictionary has been seen before:
def immutable_repr(d):
return tuple((k, tuple(v)) if isinstance(v, list) else v
for k, v in sorted(d.items()))
seen = set()
[d for d in shared_list if immutable_repr(d) not in seen and not seen.add(immutable_repr(d))]
Here immutable_repr()
takes care of producing an immutable tuple from each dictionary:
>>> immutable_repr(shared_list[0])
(u'red', ('color_thumb', ()), u'webcontent/0007/991/393/cn7991393.jpg', ('pic_uris', ((u'S', u'webcontent/0007/991/248/cn.jpg'),)), u'webcontent/0007/991/248/cn7991248.jpg')
The sorting ensures that even for dictionaries with a different key-order (which can alter based on the insertion and deletion history of the dictionary) the test for having seen it still works.
and seen
is used to track which ones have been seen so far, to filter out any subsequent duplicates:
>>> from pprint import pprint
>>> seen = set()
>>> pprint([d for d in shared_list if immutable_repr(d) not in seen and not seen.add(immutable_repr(d))])
[{'colorName': u'red',
'color_thumb': [],
'main_zoom_picture': u'webcontent/0007/991/393/cn7991393.jpg',
'pic_uris': [(u'S', u'webcontent/0007/991/248/cn.jpg')],
'swatch_image_path': u'webcontent/0007/991/248/cn7991248.jpg'}]