0

I have a python dictionary as follows

test={}
test['key1']={}
test['key1'] ['key2'] = {}
test['key1']['key2']['key3'] = 'val1'
test['key1']['key2']['key4'] = 'val2'

I have another dictionary as follows

 check = {}
    check['key1']={}
    check['key1'] ['key2'] = {}
    check['key1']['key2']['key5'] = 'val3'
    check['key1']['key2']['key6'] = 'val4'

I want to combine this dictionary so i done the following

test.update(check)

but if i do this when i try to print test dictionary it is printing like

{'key1': {'key2': {'key5': 'val3', 'key6': 'val4'}}}

but expected output is

{'key1': {'key2': {'key3': 'val1', 'key4': 'val2','key5': 'val3', 'key6': 'val4'}}}
wazza
  • 770
  • 5
  • 17
  • 42
  • obviously you won't get desire output `key2` is updated with entries of `key2` from `check` – Gahan Sep 13 '17 at 09:08
  • @Gahan can I check like key1 and key2 present in test if so if i give this expr will it work test['key1']['key2']['key5'] = 'val3' – wazza Sep 13 '17 at 09:12
  • 2
    Possible duplicate of [Update value of a nested dictionary of varying depth](https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth) – Julien Palard Sep 13 '17 at 09:17

3 Answers3

0

Here's a way to implement a deep merge :

test={}
test['key1']={}
test['key1'] ['key2'] = {}
test['key1']['key2']['key3'] = 'val1'
test['key1']['key2']['key4'] = 'val2'

check = {}
check['key1']={}
check['key1'] ['key2'] = {}
check['key1']['key2']['key5'] = 'val3'
check['key1']['key2']['key6'] = 'val4'

def deep_merge(a, b):
    for key, value in b.items():
        if isinstance(value, dict):
            # get node or create one
            node = a.setdefault(key, {})
            deep_merge(value, node)
        else:
            a[key] = value

    return a

deep_merge(test,check)
print(test)
# {'key1': {'key2': {'key3': 'val1', 'key6': 'val4', 'key5': 'val3', 'key4': 'val2'}}}

It mutates test and leaves check unmodified.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
-1

If x and y are dicts then z = {**x, **y} is a dict merging x and y

TDk
  • 1,019
  • 6
  • 18
  • user wants to keep distinct key values i.e. on merging `x = {'a': 1, 'b': {'y':4}} y = {'b': {'x':1}, 'c': 4}` as you suggested output will be `{'a': 1, 'b': {'x': 1}, 'c': 4}` but user expects `{'a': 1, 'b': {'x': 1, 'y':4}, 'c': 4}` as output – Gahan Sep 13 '17 at 09:11
-2

To deep merge dicts u need use:

def merge(source, destination):
    for key, value in source.items():
        if isinstance(value, dict):
            # get node or create one
            node = destination.setdefault(key, {})
            merge(value, node)
        else:
            destination[key] = value
    return destination

test = {'key1': {'key2': {'key3': 'val1', 'key4': 'val2'}}}
check = {'key1': {'key2': {'key6': 'val4', 'key5': 'val3'}}}

print(merge(test, check))
# {'key1': {'key2': {'key3': 'val1', 'key6': 'val4', 'key4': 'val2', 'key5': 'val3'}}}

Its a code from Python deep merge dictionary data from vincent

Thaian
  • 1,215
  • 2
  • 20
  • 30
  • user wants to keep distinct key values i.e. on merging `x = {'a': 1, 'b': {'y':4}} y = {'b': {'x':1}, 'c': 4}` as you suggested output will be `{'a': 1, 'b': {'x': 1}, 'c': 4}` but user expects `{'a': 1, 'b': {'x': 1, 'y':4}, 'c': 4}` as output – Gahan Sep 13 '17 at 09:10
  • @Thaian my expected op is {'key1': {'key2': {'key3': 'val1', 'key4': 'val2','key5': 'val3', 'key6': 'val4'}}} – wazza Sep 13 '17 at 09:15
  • @wazza check now – Thaian Sep 13 '17 at 09:31