How do I remove dicts from a list with duplicate fields in python?

dictionary, duplicates, list, python

Solution

Dump them into another dictionary, then pull them out after.

dict((x['id'], x) for x in L).values()

Problem

Let's say I have a list of dicts. I define "duplicates" as any two dicts in the list that have the same value for the field "id" (even if the other fields are different). How do I remove these duplicates. An example list would be something like: ``` [{'name': 'John' , 'id':1}, {'name': 'Mike' , 'id':5},{'name': 'Dan' , 'id':5}] ``` In this case, 'Mike' and 'Dan' would be duplicates, and one of them needs to be removed. It doesn't matter which one.

Original source

Related problems