Given a_list and b_list in python, I want to merge them so that the result is c_list.
In the example below, only want to merge elements that have a matching 'A' for the 'code' key.
a_list = [{'code': 'A', 'foo_sum': 1}, {'code': 'B', 'foo_sum': 2}]
b_list = [{'code': 'A', 'bar_sum': 3}]
c_list = [{'code': 'A', 'foo_sum': 1, 'bar_sum': 3}, {'code': 'B', 'foo_sum': 2}]
I wanted to use numpy, but could not use it because it did not necessarily match the element length and the order.
And, I tried to determine this in the iterative process, but could not achieve to make it c because local variables are not available in the comprehension notation.
for a in b_list:
if any(b["code"] == a["code"] for i, b in enumerate(b_list):
# want to use i variable, but not dified.
a["bar_sum"] = b_list[i]["code"]