I would like to deduplicate the dictionaries that contain the same "id" value.
list of dicts:
example = [{'term': 'potato', 'id': 10}, {'term': 'potatoes', 'id': 10}, {'term': 'apple', 'id': 7}]
Desired output:
example = [{'term': 'potato', 'id': 10}, {'term': 'apple', 'id': 7}]
For the moment I am only able to either remove all of the duplicates instead of keeping one; or only remove those dictionaries that are fully identical whereas I am only looking to deduplicate those that have the same id value.
example code (attempt):
import ast
new_list = []
seen_keys = set()
for term in example:
d = ast.literal_eval(term) #had to convert a string-dict to a dict first because the dictionaries were transformed to a string in a Solr database
if d['id'] not in seen_keys:
new_list.append(d)
seen_keys.add(d['id'])