python how to combine two list (key/vaule pairs), which has list of dicts

I have two lists and each list has multiple items. here is the example:

x:

[
    {
        "uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "name": "general_c",
        "generation": 0,
        "root_provider_uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "parent_provider_uuid": null
    },
    {
        "uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "name": "aggr",
        "generation": 1,
        "root_provider_uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "parent_provider_uuid": null
    },
    {
        "uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "name": "c001",
        "generation": 961,
        "root_provider_uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "parent_provider_uuid": null
    }
]

lst:

[
    [],
    [
        {
            "resource_class": "VCPU",
            "allocation_ratio": 16.0,
            "min_unit": 1,
            "max_unit": 64,

        },
        {
            "resource_class": "MEMORY_MB",
            "allocation_ratio": 1.5,
            "min_unit": 1,
            "max_unit": 257653,

        }
    ],
    [
        {
            "resource_class": "VCPU",
            "allocation_ratio": 4.0,
            "min_unit": 1,
            "max_unit": 64,

        },
        {
            "resource_class": "MEMORY_MB",
            "allocation_ratio": 2.0,
            "min_unit": 1,
            "max_unit": 257589,

        }
    ]
]

desired output:

new_list:

[
    {
        "uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "name": "general_c",
        "generation": 0,
        "root_provider_uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "parent_provider_uuid": null
        "resource_allocation: []"
    },
    {
        "uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "name": "aggr",
        "generation": 1,
        "root_provider_uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "parent_provider_uuid": null
        "resource_allocation: 
                [
                    {
                        "resource_class": "VCPU",
                        "allocation_ratio": 16.0,
                        "min_unit": 1,
                        "max_unit": 64,
            
                    },
                    {
                        "resource_class": "MEMORY_MB",
                        "allocation_ratio": 1.5,
                        "min_unit": 1,
                        "max_unit": 257653,
            
                    }
                ]
    },
    {
        "uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "name": "c001",
        "generation": 961,
        "root_provider_uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "parent_provider_uuid": null
        "resource_allocation: 
                [
                      {
                          "resource_class": "VCPU",
                          "allocation_ratio": 4.0,
                          "min_unit": 1,
                          "max_unit": 64,
                
                      },
                      {
                          "resource_class": "MEMORY_MB",
                          "allocation_ratio": 2.0,
                          "min_unit": 1,
                          "max_unit": 257589,
                
                      }
                  ]
    }
]

i want to create a new key called "resource_allocation" and add "lst" item value to it, show above

i am trying to do like this:

x["resource_allocation"] = lst
print(x)

got error:

    x["resource_allocation"] = lst
TypeError: list indices must be integers or slices, not str

i am sure i am doing it wrong, can any one suggest me the right way to achieve desired output? i have looked SO posts didn’t come across this kind of requirements before. Thanks

>Solution :

You tried to assign a key value to the list so it’s throwing an
error.You have to loop through the x list and assign keys
individually.

for i in range(len(x)):
    x[i]['resource_allocation']=lst[i]
print(json.dumps(x,indent=2))

Leave a Reply