I have a list of dicts, and I\'d like to remove the dicts with identical key and value pairs.
For this list: [{\'a\': 123}, {\'b\': 123}, {\'a\': 123}]
<
Another one-liner based on list comprehensions:
>>> d = [{'a': 123}, {'b': 123}, {'a': 123}]
>>> [i for n, i in enumerate(d) if i not in d[n + 1:]]
[{'b': 123}, {'a': 123}]
Here since we can use dict
comparison, we only keep the elements that are not in the rest of the initial list (this notion is only accessible through the index n
, hence the use of enumerate
).