I have this dictionaries list:
[{'emailAddress':'user1@test.com','role':'writer','displayName':'USER1','type':'user','resultRole':'writer'},
{'emailAddress':'user2@test.com','role':'owner','displayName':'USER2','type':'user','resultRole':'owner'},
{'emailAddress':'user1@test.com','role':'writer','displayName':'USER1','type':'user','resultRole':'multiplex'},
{'emailAddress':'user2@test.com','role':'owner','displayName':'USER2','type':'user','resultRole':'owner'},
{'emailAddress':'user3@test.com','role':'commenter','displayName':'USER3','type':'user','resultRole':'multiplex'}]
I want to remove the duplicate dictionaries in the list based on the user's email address (good so far), the problem I have is that if the user is duplicated and the "resultRole" key has the value "multiplex", it remains and the other duplicate is removed.
the result i want:
[{'emailAddress':'user2@test.com','role':'owner','displayName':'USER2','type':'user','resultRole':'owner'},
{'emailAddress':'user1@test.com','role':'writer','displayName':'USER1','type':'user','resultRole':'multiplex'},
{'emailAddress':'user3@test.com','role':'commenter','displayName':'USER3','type':'user','resultRole':'multiplex'}]
how could i do it?
At the moment I only remove the duplicates with this code:
seen = set()
permissionsUsers_list = []
for d in docResults:
t = tuple(d.items())
if t[1] not in seen:
seen.add(t[1])
permissionsUsers_list.append(d)
source https://stackoverflow.com/questions/76317731/python-remove-duplicates-in-a-dictionary-list-based-on-2-values
Comments
Post a Comment