<qsqgeekyog...@tiscali.co.uk> wrote in message news:16014207.1237036582618.javamail.r...@ps26...
hi,
i have a list which contains duplicate dictionaries.

how do i extract the unique items out?

l1 = [{'a': 'ddd'}, {'a': 'ddd'}, {'b': 'eee'}, {'c': 'ggg'}]
set(l1)
TypeError: dict objects are unhashable

but,
{'a': 'ddd'} == {'a': 'ddd'}
True

How about:

l1 = [{'a': 'ddd'}, {'a': 'ddd'}, {'b': 'eee'}, {'c': 'ggg'}]
[dict(n) for n in set(tuple(n.items()) for n in l1)]
[{'c': 'ggg'}, {'b': 'eee'}, {'a': 'ddd'}]

-Mark


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to