Home > Net >  How to finish this python dictionary transformation?
How to finish this python dictionary transformation?

Time:02-26

I have a python dictionary parsed_dict.

{
    "Field name 1": {
        "item1": -0.05,
        "item2": -0.06,
        "item3": -0.07,
    },
    "Field name 2": {
        "item1": -0.01,
        "item2": -0.02,
        "item3": -0.03,
    },
    "Field name 3": {
        "item1": -0.05,
        "item2": -0.06,
        "item3": -0.07,
    }
}

I would like to transform it into another dictionary new_dict that looks like this;

{
    "fields": [
        {
            "key": "name",
            "label": "name",
            "sortable": true
        },  
        {
            "key": "item1",
            "label": "item1",
            "sortable": true
        },
        {
            "key": "item2",
            "label": "item2",
            "sortable": true
        },
        {
            "key": "item3",
            "label": "item3",
            "sortable": true
        }
    ],
    "items": [
        {
            "name": "Field name 1"  
            "item1": -0.05,
            "item2": -0.06,
            "item3": -0.07
        },
        {
            "name": "Field name 2"  
            "item1": -0.01,
            "item2": -0.02,
            "item3": -0.03
        },
        {
            "name": "Field name 3"  
            "item1": -0.05,
            "item2": -0.06,
            "item3": -0.07
        }
    ]
}

I finally managed to find almost the right solution to fill up "items". Even then, my solution is not 100% right because the "name" is at the bottom of "items", not at the top. Here is my code;

for value in parsed_dict.values():
    new_dict["items"].append(value)
for i, key in enumerate(parsed_dict.keys()):
    new_dict["items"][i]["name"] = key

I am stuck for a long time at figuring out how to fill up "fields".

I am using python 3.9

CodePudding user response:

The answer would be very close to your previous question's answers. The only difference is to assign the key value to the value["name"] in the loop. You can try what follows:

new_dict = {"fields":[], "items":[]}
for key, value in parsed_dict.items():
  tempFieldDict = {"key":key, "label":key, "sortable": "true"}
  new_dict["fields"].append(tempFieldDict)
  value["name"] = key
  new_dict["items"].append(value)
new_dict

The new_dict variable will result in the following output:

{
    "fields": [
        {
            "key": "Field name 1",
            "label": "Field name 1",
            "sortable": "true"
        },
        {
            "key": "Field name 2",
            "label": "Field name 2",
            "sortable": "true"
        },
        {
            "key": "Field name 3",
            "label": "Field name 3",
            "sortable": "true"
        }
    ],
    "items": [
        {
            "item1": -0.05,
            "item2": -0.06,
            "item3": -0.07,
            "name": "Field name 1"
        },
        {
            "item1": -0.01,
            "item2": -0.02,
            "item3": -0.03,
            "name": "Field name 2"
        },
        {
            "item1": -0.05,
            "item2": -0.06,
            "item3": -0.07,
            "name": "Field name 3"
        }
    ]
}

CodePudding user response:

You can try this:

for key, value in parsed_dict.items():
   n = dict({})
   for k, v in value.items():
     n.update({k: v})

   new_dict["items"] = { "name": key}
   new_dict["items"].update(n)
  • Related