Home > front end >  remove duplicate dictionary python
remove duplicate dictionary python

Time:03-09

I have problem, I have list like this:

[{'id': 34, 'questionid': 5, 'text': 'yes', 'score': 1}, 
 {'id': 10, 'questionid': 5, 'text': 'test answer updated', 'score': 2}, 
 {'id': 20, 'questionid': 5, 'text': 'no', 'score': 0}, 
 {'id': 35, 'questionid': 5, 'text': 'yes', 'score': 1}]

and I want remove duplicate "questionid", "text" and "score", for example in this case i want output like this:

[{'id': 34, 'questionid': 5, 'text': 'yes', 'score': 1}, 
 {'id': 10, 'questionid': 5, 'text': 'test answer updated', 'score': 2}, 
 {'id': 20, 'questionid': 5, 'text': 'no', 'score': 0}]

how can I get this output in python?

CodePudding user response:

We could create dictionary that has "questionid", "text" and "score" tuple as key and dicts as values and use this dictionary to check for duplicate values in data:

from operator import itemgetter
out = {}
for d in data:
    key = itemgetter("questionid", "text", "score")(d)
    if key not in out:
        out[key] = d
out = list(out.values())

Output:

[{'id': 34, 'questionid': 5, 'text': 'yes', 'score': 1},
 {'id': 10, 'questionid': 5, 'text': 'test answer updated', 'score': 2},
 {'id': 20, 'questionid': 5, 'text': 'no', 'score': 0}]
  • Related