Merging dictionary, of subset keys, into full key-value pair dictionary

Viewed 27

I have 2 dictionaries. I want to merge or transpose them together. I've read many posts and still couldn't find exactly what I was looking for; so below is an example.

a is empty, but has all key structures in place.

a = {'id_1': {'Biographic data': {'Name': '',
   'Aliases': '',
   'Date of birth': '',
   'Phone numbers': '',
  'Prosecution data': {'Charges': '',
   'Case dates': '',
   'Verdicts': '',
   'Subject information': '',
   'Attorney information': '',
   'Judge information': '',
   'Sentencing information': '',
   'Release dates': ''}},
 'id_2': {'Biographic data': {'Name': '',
   'Aliases': '',
   'Date of birth': '',
   'Phone numbers': '',
  'Prosecution data': {'Charges': '',
   'Case dates': '',
   'Verdicts': '',
   'Subject information': '',
   'Attorney information': '',
   'Judge information': '',
   'Sentencing information': '',
   'Release dates': ''}}}

b only has the keys, and its parent keys, for which it has values for.

b = {'id_1': {'Prosecution data': {'Sentencing information': "three-months imprisonment"}},
 'id_2': {'Prosecution data': {'Sentencing information': "the death of that person during the period immediately prior to the commission of the offense as established"}}}

I want this (where e.g. id_<x>, Prosecution data, Sentencing information has been appended for both id_1 and id_2):

a = {'id_1': {'Biographic data': {'Name': '',
   'Aliases': '',
   'Date of birth': '',
   'Phone numbers': '',
  'Prosecution data': {'Charges': '',
   'Case dates': '',
   'Verdicts': '',
   'Subject information': '',
   'Attorney information': '',
   'Judge information': '',
   'Sentencing information': "three-months imprisonment",
   'Release dates': ''}},
 'id_2': {'Biographic data': {'Name': '',
   'Aliases': '',
   'Date of birth': '',
   'Phone numbers': '',
  'Prosecution data': {'Charges': '',
   'Case dates': '',
   'Verdicts': '',
   'Subject information': '',
   'Attorney information': '',
   'Judge information': '',
   'Sentencing information': "the death of that person during the period immediately prior to the commission of the offense as established",
   'Release dates': ''}}}

1 Answers

The a and b dict have some bugs on quote, I already fix it.

code:

a = {'id_1': {'Biographic data': {'Name': '',
   'Aliases': '',
   'Date of birth': '',
   'Phone numbers': '',
  'Prosecution data': {'Charges': '',
   'Case dates': '',
   'Verdicts': '',
   'Subject information': '',
   'Attorney information': '',
   'Judge information': '',
   'Sentencing information': '',
   'Release dates': ''}}},
 'id_2': {'Biographic data': {'Name': '',
   'Aliases': '',
   'Date of birth': '',
   'Phone numbers': '',
  'Prosecution data': {'Charges': '',
   'Case dates': '',
   'Verdicts': '',
   'Subject information': '',
   'Attorney information': '',
   'Judge information': '',
   'Sentencing information': '',
   'Release dates': ''}}}
}
b = {'id_1': {'Biographic data': {'Prosecution data': {'Sentencing information': "three-months imprisonment"}}},
 'id_2': {'Biographic data': {'Prosecution data': {'Sentencing information': "the death of that person during the period immediately prior to the commission of the offense as established"}}}}
import collections.abc

def update(d, u):
    for k, v in u.items():
        if isinstance(v, collections.abc.Mapping):
            d[k] = update(d.get(k, {}), v)
        else:
            d[k] = v
    return d
print(update(a,b))

result:

{
    "id_1": {
        "Biographic data": {
            "Name": "",
            "Aliases": "",
            "Date of birth": "",
            "Phone numbers": "",
            "Prosecution data": {
                "Charges": "",
                "Case dates": "",
                "Verdicts": "",
                "Subject information": "",
                "Attorney information": "",
                "Judge information": "",
                "Sentencing information": "three-months imprisonment",
                "Release dates": "",
            },
        }
    },
    "id_2": {
        "Biographic data": {
            "Name": "",
            "Aliases": "",
            "Date of birth": "",
            "Phone numbers": "",
            "Prosecution data": {
                "Charges": "",
                "Case dates": "",
                "Verdicts": "",
                "Subject information": "",
                "Attorney information": "",
                "Judge information": "",
                "Sentencing information": "the death of that person during the period immediately prior to the commission of the offense as established",
                "Release dates": "",
            },
        }
    },
}
Related