Home > Back-end >  How to add multiple nested dictionaries at once?
How to add multiple nested dictionaries at once?

Time:03-23

I have a method which requires being able to add multiple nested dictionaries at once.

For example:

mydict = {}
mydict['subdict']['subdict2'] = {'this': 'is what i want'}

How can I do this?

CodePudding user response:

from collections import defaultdict
mydict = defaultdict(dict)
mydict['subdict']['subdict2'] = {'this': 'is what i want'}
  • Related