Python add dictionary to existing dictionary

What am I doing wrong here? Adding inside the dictionary doesn't seem to work

final = [] topid = { "ida" : "ida", "idb" : "idb", "idc" : "idc", "subid" : {} } for subid in subids: insubid = { "name" : subid.name, "sida" : "sida", "sidb" : "sidb", "sidc" : "sidc", } topid["subid"].append(insubid) final.append(topid) 

I get an error message:

AttributeError: object 'dict' does not have attribute 'append'

+4
source share
2 answers

I'm not sure if this is what you want, but using append , your code expects the subid be a list. If you do this, you can change this:

 topid = { "ida" : "ida", "idb" : "idb", "idc" : "idc", "subid" : {} } 

:

 topid = { "ida" : "ida", "idb" : "idb", "idc" : "idc", "subid" : [] } 

Note that subid now an empty list, not a dictionary.

+11
source

If I get it right, all you have to do is change:

 topid["subid"].append(insubid) 

in

 topid["subid"] = insubid 
+7
source

Source: https://habr.com/ru/post/1382738/


All Articles