简体   繁体   中英

Python - add a list of dictionary in a dictionary

So I have a dictionary called component and a list of dictionaries called allocations. I want to be able to put the allocations under a component as a nested dictionary . kind of like so:

Allocations[
    allocation1 : {
        key : value
    },
    allocation2  {
        key : value
    }
]

My desired output:

Component1 : {
   key:value
   allocations : [allocation1 : {
                            key : value
                         }
                 ,allocation2 : {
                            key : value
                         }
               ]
}

I came from Java, and i realize there is no append that I can use. I tried this and obviously didnt work:

            #allocate this under the selected component - DIDNT WORK
            component["allocations"][] = allocation

How can I create a list of dictionaries in a dictionary?

Simply assign it:

component["allocations"] = some_list

For instance, if you want a new, empty one:

component["allocations"] = []

or:

component["allocations"] = list()

Then, manipulate the list as usual:

component["allocations"].append(some_object)

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