简体   繁体   中英

Python update list with dict values

This question seems basic but I did not find a clean solution for it.

I want to add some values (dictionary) to one list, eg

data = { "item" : {}, "series": [ ] }
...
data['item'].update(dict(labels=myitems))   
data['series'].append( dict(data=myseriesData) )       
data['series'].append( dict(name=myname) )

This produces:

{'series': [{'data': [1, 2, 3]}, {'name': 'serie1'}], 'item': {'labels': ['jan', 'feb', 'march']}}

Although I don't what the curly-braces in the series list, my JSON needs to be in the following format:

{'series': ['data': [1, 2, 3], 'name': 'serie1'], 'item': {'labels': ['jan', 'feb', 'march']}}

I've tried different approaches like using extend update add a string or tuple but without success..

Any idea? what is the "trick"?

仅附加一个dict

data['series'].append(dict(data=myseriesData, name=myname))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM