0

I have a problem, I have a 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?

1 Answer 1

1

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}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.