Possible Duplicate:
how to uniqify a list of dict in python
I have a list as follows
[
{'x':'1','y':'1'},{'x':'2','y':'2'},{'x':'1','y':'1'}
]
I want to get a new list as follows (unique elements)
[
{'x':'1','y':'1'},{'x':'2','y':'2'}
]
What’s the best way?
Usually an easy solution for keeping unique elements is to add them to a set. However, since a dict is unhashable (can’t be put in a set), I provided a workaround. First, the dicts are converted to strings, placed in a set (to keep the unique ones), and then converted back to dicts using ast.literal_eval.