Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    [],
    [
        {
            "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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading