from itertools import chain
a = [{'code': 'a1', 'total1': 7}, {'code': 'a2', 'total1': 17}]
b = [{'code': 'a1', 'total2': 55}, {'code': 'a3', 'total2': 22}]
def f(a, b):
cache = {}
for dct in chain(a, b):
code = dct["code"]
cache.setdefault(code, dct).update(dct)
return list(cache.values())
f(a, b)