0

I would like to update or create a nested dictionary without altering the existing content.

In the example below I will try to add the couple {'key2.2': 'value2.2'} nested as a child to the entry key2.

  • Case 1: the parent key (key2) exists :
    mydict = {'key1': 'value1', 'key2': {'key2.1': 'value2.1'}}
    mydict['key2'].update({'key2.2': 'value2.2'})
    pprint(mydict)

{'key1': 'value1', 'key2': {'key2.1': 'value2.1', 'key2.2': 'value2.2'}}

This is what I expected, no problem here.

  • Case 2: the parent key (key2) does not exists :*
    mydict = {'key1': 'value1'}
    mydict['key2'].update({'key2.2': 'value2.2'})

KeyError: 'key2'

Seems logic, so let's try something else..

    mydict = {'key1': 'value1'}
    mydict.update({'key2': {'key2.2': 'value2.2'}})
    pprint(mydict)

{'key1': 'value1', 'key2': {'key2.2': 'value2.2'}}

Perfect ! Let's check if this works with case 1 too.

  • Case 1B: the parent key (key2) exists :
    mydict = {'key1': 'value1', 'key2': {'key2.1': 'value2.1'}}
    mydict.update({'key2': {'key2.2': 'value2.2'}})
    pprint(mydict)

{'key1': 'value1', 'key2': {'key2.2': 'value2.2'}}

The existing entry {'key2.1': 'value2.1'} got deleted, this is not what I want.

What would be the best way to achieve what I want ? Like a nested update ? I know this would be possible with a couple of try:/except KeyError:, but it doesn't seems very clean.

2
  • Isn't case 1 already does what you want? Commented May 12, 2020 at 12:26
  • I don't know in advance if I will be in the case 1 or case 2 situation, so I need a way to achieve the expected outcome no matter the case ! Commented May 12, 2020 at 12:28

3 Answers 3

3

You could use defaultdict:

from collections import defaultdict

mydict = defaultdict(dict)
mydict.update({'key1': 'value1', 'key2': {'key2.1': 'value2.1'}})
mydict['key2'].update({'key2.2': 'value2.2'})
print(mydict)

mydict = defaultdict(dict)
mydict.update({'key1': 'value1'})
mydict['key2'].update({'key2.2': 'value2.2'})
print(mydict)

Outputs:

defaultdict(<class 'dict'>, {'key1': 'value1', 'key2': {'key2.1': 'value2.1', 'key2.2': 'value2.2'}})
defaultdict(<class 'dict'>, {'key1': 'value1', 'key2': {'key2.2': 'value2.2'}})
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

mydict = {'key1': 'value1', 'key2': {'key2.1': 'value2.1'}}
if "key2" in mydict:
    mydict["key2"].update({'key2.2': 'value2.2'})
else:
    mydict["key2"] = {'key2.2': 'value2.2'}

Comments

0

Code:

mydict = {'key1': 'value1', 'key2': {'key2.1': 'value2.1'}}
mydict['key2.2'.split('.')[0]].update({'key2.2' : 'value2.2'})
print(mydict)

Output:

{'key2': {'key2.2': 'value2.2', 'key2.1': 'value2.1'}, 'key1': 'value1'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.