2

I would like to merge these two nested dictionaries :

dict1 = {'NYSE': {'day': {'USD': {'missing': ['z', 'y', 'x']}}}}
dict2 = {'NYSE': {'hour': {'USD': {'missing': ['z', 'y', 'x']}}}}

So the result would be something like this :

{
    'NYSE': {
        'day': {
            'USD': {
                'missing': ['z', 'y', 'x']
            }
        },
        'hour': {
            'USD': {
                'missing': ['z', 'y', 'x']
            }
        }
    }
}

In some situations I need to merge the second level of the dictionaries like this :

dict1 = {'NYSE': {'day': {'USD': {'missing': ['z', 'y', 'x']}}}}
dict3 = {'NYSE': {'day': {'EUR': {'missing': ['z', 'y', 'x']}}}}

Is there a simple way to do this ?

When I use dict1.update(dict2) it doesn't gives the expected result because dict2 becomes dict1.

I could do this but it is not flexible at all.

dict1['NYSE']['hour'] = {'USD': {'missing': ['z', 'y', 'x']}}

Thank you,

Florent
  • 1,791
  • 22
  • 40

1 Answers1

3

You can use:

dict1['NYSE'].update(dict2['NYSE'])