python - How to add new objects to nested dictionary? -
given below object, how add new city "losangeles" in "california"? given country, state, city , attributes city, how can update object? (i pretty new python)
myplaces = { "unitedstates": { "california": { "sanfrancisco": { "description": "tech heaven", "population": 1234, "keyplaces": ["someplace1", "someplace2"] } } }, "india": { "karnataka": { "bangalore": { "description": "it hub of india", "population": 12345, "keyplaces": ["jpnagar", "brigade"] }, "mysore": { "description": "hostoric place", "population": 12345, "keyplaces": ["mysorepalace"] } } } }
i tried adding elements below (like how done in php):
myplaces['unitedstates']['california']['losangeles']['description'] = 'entertainment'
edit: not duplicate. looking generic way add items.
you've got -
myplaces['unitedstates']['california']['losangeles'] = {'description':'entertainment'}
Comments
Post a Comment