I would like to have a dictionary appended with new dictionaries.
t = {}
t['bush_mod']={} # Toplevel
ana1 = {}
ana1['ana1_lin_001']={}
t['bush_mod'] = ana1 # Add below Toplevel
ana2 = {}
ana2['ana2_lin_001'] = {}
ana2['ana2_lin_002'] = {}
t['bush_mod'] = ana2 # Add below Toplevel
When I add ana2
the dictionary t
gets overwritten which i do not want.
OUT: {'bush_mod': {'ana2_lin_001': {}, 'ana2_lin_002': {}}}
I wanted to have ana2
as the second dict (appended) below the Toplevel.
OUT: {'bush_mod': {'ana1_lin_001': {}},{'ana2_lin_001': {}, 'ana2_lin_002': {}}}
It would really be helpful if someone could help me with the syntax.
Thanks in advance!
If you want to stick to nested dictionaries I think dict.update
might be an option:
t = {}
t['bush_mod']={}
ana1 = {}
ana1['ana1_lin_001']={}
ana2 = {}
ana2['ana2_lin_001'] = {}
ana2['ana2_lin_002'] = {}
t['bush_mod'].update(ana1)
t['bush_mod'].update(ana2)
Such that t
is:
{'bush_mod': {'ana1_lin_001': {}, 'ana2_lin_001': {}, 'ana2_lin_002': {}}}
dict.update
here adds the keys-value pairs from ana1
and ana2
to the dictionary t['bush_mod
]`
you need to use distinct keys for each element in the dict t... it is overriding because you used 'bush_mod' twice...
you need to make t['bush_mod']= [] then append that list with things.
t['bush_mod'] = []
t['bush_mod'].append(ana1)
t['bush_mod'].append(ana2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.