How does python generate a nested dictionary? I don't want to use defaultdict implementation.

such as native dict attribute

test=dict()
test["a"]=1         -sharp 
test["x"]["y"]=2    -sharp 

I want to generate a recursive dictionary. If you don"t have this key, create it. Which method can be simpler and more elegant? thank you

Sep.30,2021

there will be some problems.
test ["x"] ["y"] is equivalent to a chain call. First get test ["x"] , and then assign to ["y"] this key. When you take the first step, you can't get whether you want to assign values later. You can only create keys that don't exist every time you getitem.
what type of key is created? The default is dict, of course, but if his y position is an integer, do you want to create a list?
you have to consider all these.
finally is a simple implementation. Do not consider the case of list at all.


:

test=dict()
test['a']=1
x = {"y":2}         
test['x'] = x   
Menu