Consider this short list of python dictionaries (the first element of the dictionary is a string, the second element is a Widget object):
raw_results =
[{'src': 'tag', 'widget': <Widget: to complete a form today>},
{'src': 'tag', 'widget': <Widget: a newspaper>},
{'src': 'zip', 'widget': <Widget: to complete a form today>},
{'src': 'zip', 'widget': <Widget: the new Jack Johnson album>},
{'src': 'zip', 'widget': <Widget: a newspaper>},
{'src': 'zip', 'widget': <Widget: premium dog food >}]
I want to view this list and remove duplicates that this question answered me with:
Remove duplicates in a list keeping its order (Python)
known_widgets= set()
processed_results = []
for x in raw_results:
widget = x['widget']
if widget in known_widgets:
continue
else:
processed_results.append(x)
known_widgets.add(widget)
However, after deleting the duplicate line (for example, dupe 1b) I want to change the remaining duplicates (for example, dupe 1a) to "src". I would like to add deleted src duplicates to the original. Here is what I would like to receive:
processed_results =
[{'src': 'tag-zip', 'widget': <Widget: to complete a form today>},
{'src': 'tag-zip', 'widget': <Widget: a newspaper>},
{'src': 'zip', 'widget': <Widget: the new Jack Johnson album>},
{'src': 'zip', 'widget': <Widget: premium dog food >}]
I am sure it is easy to do, but my head is spinning after too much coffee and spinning for many hours over this problem. I would really like and really appreciate the help of a specialist. Thank!