How to add a new dictionary inside another dictionary?

Asked

Viewed 529 times

2

ini={"MAIN":{}}
ini['MAIN']['Nome']="Jaque"
print ini

Print output: {'MAIN': {'Nome': 'Jaque'}}

How to put a new dictionary into what already exists?

I want to have it: {'MAIN': {'Nome': 'Jaque'} 'NOVO_DICT': {} }

  • Would not be ini['NOVO_DICT'] = {}? I couldn’t quite understand the doubt.

  • Or else ini.update( { 'NOVO_DICT':{} } ).

1 answer

0


I believe you want to have it:

{'MAIN': {'Nome': 'Jaque'}, 'NOVO_DICT': {}} #vígula antes de 'NOVO_DICT'

If I’m right, so just add a new value to the entry NOVO_DICT of the variable ini. Thus:

ini={"MAIN":{}}
ini['MAIN']['Nome']="Jaque"
ini['NOVO_DICT']={}
print ini

In assigning {} at the entrance NOVO_DICT of the variable ini, Python automatically already creates this entry if it does not exist.

  • that’s right! Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.