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

How to transform this python dictionary to this other dictionary format?

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": "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
        },
        {
            "item1": -0.01,
            "item2": -0.02,
            "item3": -0.03
        },
        {
            "item1": -0.05,
            "item2": -0.06,
            "item3": -0.07
        }
    ]
}

Here is what I did so far.

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

new_dict = {}
for idx, key in enumerate(parsed_dict.keys()):
    new_dict['fields'][idx]['key'] = parsed_dict[key]
    new_dict['fields'][idx]['label'] = parsed_dict[key]
    new_dict['fields'][idx]['sortable'] = "true"

I am already stuck at this stage. The error I get is KeyError: 'fields'.

I am using python 3.9

>Solution :

There are a few problems with your code I would like to mention:

First of all you can’t call new_dict["fields"][idx] since it doesn’t exist in the new_dict dictionary.

Second, personally speaking, I would not use enumerate in your case, rather I would like to use items on the dictionary.

Therefore, I came up with the code below:

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,
    }
}
resultDict = {"fields":[], "items":[]}
new_dict = {"fields":[], "items":[]}
for key, value in parsed_dict.items():
  tempFieldDict = {"key":key, "label":key, "sortable": "true"}
  new_dict["fields"].append(tempFieldDict)
  new_dict["items"].append(value)
new_dict

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},
  {'item1': -0.01, 'item2': -0.02, 'item3': -0.03},
  {'item1': -0.05, 'item2': -0.06, 'item3': -0.07}
   ]
}
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