Home > Mobile >  How to change format of a nested dictionary into a list of dictionaries?
How to change format of a nested dictionary into a list of dictionaries?

Time:03-28

I have a nested dictionary, d1:

{'2020-12-10': {'EUR': 1.1379,
  'JPY': 128.75,
  'BGN': 1.9558,
  'CZK': 25.845,
  'DKK': 7.4641,
  'GBP': 0.90228},
 '2020-12-09': {'EUR': 1.1354,
  'JPY': 128.31,
  'BGN': 1.9558,
  'CZK': 25.886,
  'DKK': 7.463,
  'GBP': 0.88885},
 '2020-11-08': {'EUR': 1.1409,
  'JPY': 129.04,
  'BGN': 1.9558,
  'CZK': 26.002,
  'DKK': 7.4617,
  'GBP': 0.89108}}

and I want to change it into this format:

d2 = [{'date': '2020-12-10', 'target_currency': 'EUR', 'exchange_rate' : 1.1379},
        {'date': '2020-12-09', 'target_currency': 'EUR', 'exchange_rate' : 1.1354},
         ...
        ]

(which I need to convert later in a line-delimited json file, using json.dumps()).

I have tried this as a first step, which gets the date as a key.

d2 = {}
for k, v in d1.items():                     
    key = k
    d2[key] = {"date": k}   
  
    d2[key].update({k_:v_ for k_, v_ in v.items()})
    
d2   

{'2020-12-10': {'date': '2020-12-10',
  'EUR': 1.1379,
  'JPY': 128.75,
  'BGN': 1.9558,
  'CZK': 25.845,
  'DKK': 7.4641,
  'GBP': 0.90228},

Need some pointers how to go from here. thank you.

CodePudding user response:

Try:

dct = {
    "2020-12-10": {
        "EUR": 1.1379,
        "JPY": 128.75,
        "BGN": 1.9558,
        "CZK": 25.845,
        "DKK": 7.4641,
        "GBP": 0.90228,
    },
    "2020-12-09": {
        "EUR": 1.1354,
        "JPY": 128.31,
        "BGN": 1.9558,
        "CZK": 25.886,
        "DKK": 7.463,
        "GBP": 0.88885,
    },
    "2020-11-08": {
        "EUR": 1.1409,
        "JPY": 129.04,
        "BGN": 1.9558,
        "CZK": 26.002,
        "DKK": 7.4617,
        "GBP": 0.89108,
    },
}

d2 = [
    {"date": k1, "target_currency": k2, "exchange_rate": v2}
    for k1, v1 in dct.items()
    for k2, v2 in v1.items()
]

print(d2)

Prints:

[
    {"date": "2020-12-10", "target_currency": "EUR", "exchange_rate": 1.1379},
    {"date": "2020-12-10", "target_currency": "JPY", "exchange_rate": 128.75},
    {"date": "2020-12-10", "target_currency": "BGN", "exchange_rate": 1.9558},
    {"date": "2020-12-10", "target_currency": "CZK", "exchange_rate": 25.845},
    {"date": "2020-12-10", "target_currency": "DKK", "exchange_rate": 7.4641},
    {"date": "2020-12-10", "target_currency": "GBP", "exchange_rate": 0.90228},
    {"date": "2020-12-09", "target_currency": "EUR", "exchange_rate": 1.1354},
    {"date": "2020-12-09", "target_currency": "JPY", "exchange_rate": 128.31},
    {"date": "2020-12-09", "target_currency": "BGN", "exchange_rate": 1.9558},
    {"date": "2020-12-09", "target_currency": "CZK", "exchange_rate": 25.886},
    {"date": "2020-12-09", "target_currency": "DKK", "exchange_rate": 7.463},
    {"date": "2020-12-09", "target_currency": "GBP", "exchange_rate": 0.88885},
    {"date": "2020-11-08", "target_currency": "EUR", "exchange_rate": 1.1409},
    {"date": "2020-11-08", "target_currency": "JPY", "exchange_rate": 129.04},
    {"date": "2020-11-08", "target_currency": "BGN", "exchange_rate": 1.9558},
    {"date": "2020-11-08", "target_currency": "CZK", "exchange_rate": 26.002},
    {"date": "2020-11-08", "target_currency": "DKK", "exchange_rate": 7.4617},
    {"date": "2020-11-08", "target_currency": "GBP", "exchange_rate": 0.89108},
]
  • Related