How to remove a dict from a list with duplicate fields in python?

Say I have a list of dicts. I define "duplicates" as any two dicts in the list that have the same value for the "id" field (even if the other fields are different). How to remove these duplicates.

Example example:

[{'name': 'John' , 'id':1}, {'name': 'Mike' , 'id':5},{'name': 'Dan' , 'id':5}]

In this case, "Mike" and "Dan" will be duplicated, and one of them must be deleted. It doesn't matter which one.

+5
source share
2 answers

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

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

The following understanding of the feature list should solve your problem.

def f(seq):
    s = set()
    return [x for x in seq if x['id'] not in s and not s.add(x['id'])]
+2
source

All Articles