I tried to google and find a problem which is very similar to my use case here: combine dictionaries in list of dictionaries based on matching key:value pair. But it seems that it did not correspond 100% to my case as I have list of nested dictionaries. Suppose that I have a list of nested dictionaries (more than 2) but in this case I considered two nested dictionaries to make the example:
my_list = [{'sentence': ['x',
'ray',
'diffractometry',
'has',
'been',
'largely',
'used',
'thanks',
'to',
],
'mentions': [{'mention': [27, 28],
'positives': [26278, 27735, 21063],
'negatives': [],
'entity': 27735}]},
{'sentence': ['x',
'ray',
'diffractometry',
'has',
'been',
'largely',
'used',
'thanks',
'to',
],
'mentions': [{'mention': [13, 14],
'positives': [7654],
'negatives': [],
'entity': 7654}]}]
How can I merge these two dictionaries based on the matching of key(sentence) and value(list of all tokens) So that I can get the desired result as below:
my_new_list = [
{'sentence': ['x',
'ray',
'diffractometry',
'has',
'been',
'largely',
'used',
'thanks',
'to',
],
'mentions': [
{'mention': [27, 28],
'positives': [26278, 27735, 21063],
'negatives': [],
'entity': 27735
},
{'mention': [13, 14],
'positives': [7654],
'negatives': [],
'entity': 7654
}
]
}
]
How to merge the list of key "mentions" when matching the key(sentence):value(list of all tokens)? In my actual list, there will be a lot of dictionaries with the same style.
Many thanks for your help.