Generalized version to use as many dicts as you want.
All values of the dicts must be dicts, otherwise this code won't work.
All values of the dicts must be dicts, otherwise this code won't work.
import pprint d1 = { "10084": { "aliases": { "first_last": "Loren Meyer", "last_first": "Meyer, Loren" } }, "10111": { "aliases": { "first_last": "Cory Higgins", "last_first": "Higgins, Cory" } }, "10163": { "aliases": { "first_last": "Antoine Wright", "last_first": "Wright, Antoine" } } } #vitals dict d2 = { "10084": { "vitals": { "ht": 82, "wt": 257, "birth_date": "12/30/1972" } }, "10111": { "vitals": { "ht": 77, "wt": 180, "birth_date": "6/14/1989" } }, "10163": { "vitals": { "ht": 79, "wt": 210, "birth_date": "2/6/1984" } } } #combine dict d3 = { "10084": { "combine": { "span": 81.0, "reach": 100.0, "body_fat": 4.1, } }, "10111": { "combine": { "span": 81.0, "reach": 100.0, "body_fat": 9.1, } }, "10163": { "combine": { "span": 75.0, "reach": 85.0, "body_fat": 6.1, } } } def combine_subdicts(*dicts): if len(dicts) < 2: raise ValueError('Minimum two dicts are required to combine') result = {} for uid, data in dicts[0].items(): try: for subdict in dicts[1:]: data.update(subdict[uid]) except KeyError: print(f'Inconsistent. Missing id {uid} in {subdict}') continue result[uid] = data return result combined_dict = combine_subdicts(d1, d2, d3) pprint.pprint(combined_dict, indent=2)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!