Home > database >  How to transform this python dictionary to this other dictionary format?
How to transform this python dictionary to this other dictionary format?

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": "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.

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

CodePudding user response:

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}
   ]
}

CodePudding user response:

There may be a more elegant way to do this but I think this is functionally correct:

newdict = {'fields': [{'key': fn, 'label': fn, 'sortable': True} for fn in parsed_dict],
           'items': [{'item1': v['item1'], 'item2': v['item2'], 'items3': v['item3']} for v in parsed_dict.values()]}
  • Related