Append dict items to a dict in Python

vowag13080

I have a dictionary, and I want to add items of the dictionary type as well...

dict = {}

I do this:

dict['item1'] = { "prop1": "XXX"}
dict['item1'] = { "prop2": "XXX"}

Expected result:

{"item1" : {"prop1": "XXX", "prop2": "XXX"}}
Mureinik

I'd use a defaultdict and update the element instead of assigning to it:

from collections import defaultdict
d = defaultdict(dict)
d['item1'].update({"prop1": "XXX"})
d['item1'].update({"prop2": "XXX"})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related