Given the following list of dictionaries:
[{'person_id': '10114', 'detail_info': [1286, 1]}, {'person_id': '10114', 'detail_info': [1286, 1]}, {'person_id': '10114', 'detail_info': [1286, 3]}]
I need this result:
[{'person_id': '10114', 'detail_info': [1286, 5]}]
I need to add the second element of each detail_info
when merging.
CodePudding user response:
Assuming you want to merge on 'person_id' (if not, you can easily change the key):
out = {}
for d in l:
key = d['person_id']
if not key in out:
out[key] = d
else:
out[key]['detail_info'][1] = d['detail_info'][1]
out = list(out.values())
output:
[{'person_id': '10114', 'detail_info': [1286, 5]}]
input:
l = [{'person_id': '10114', 'detail_info': [1286, 1]},
{'person_id': '10114', 'detail_info': [1286, 1]},
{'person_id': '10114', 'detail_info': [1286, 3]}]