Home > database >  Add another layer on top of existing dictionary?
Add another layer on top of existing dictionary?

Time:09-06

I have a dict_sample structured like this:

{'close': {'01/01/2022': 70327.6216, '02/01/2022': 69699.74376000001, '03/01/2022': 68400.97064, '04/01/2022': 67238.73537000001, '05/01/2022': 63901.21185000001, '06/01/2022': 63857.56575, '07/01/2022': 61243.99047, '08/01/2022': 61428.6952},
 'high': {'01/01/2022': 70592.20832, '02/01/2022': 70632.42668, '03/01/2022': 70059.4992, '04/01/2022': 69685.67126, '05/01/2022': 69189.04710000001, '06/01/2022': 64857.41691000001, '07/01/2022': 63574.54257, '08/01/2022': 62360.916040000004},
 'low': {'01/01/2022': 68087.621, '02/01/2022': 68825.25224, '03/01/2022': 67331.1328, '04/01/2022': 66893.72049, '05/01/2022': 62590.39965, '06/01/2022': 62944.24587, '07/01/2022': 60158.021, '08/01/2022': 59810.57563},
 'open': {'01/01/2022': 68087.621, '02/01/2022': 70328.65284, '03/01/2022': 69673.37348000001, '04/01/2022': 68116.52833, '05/01/2022': 67443.40665, '06/01/2022': 64352.60172, '07/01/2022': 63530.61439, '08/01/2022': 61250.77133},
 'volume': {'01/01/2022': 31239, '02/01/2022': 27020, '03/01/2022': 41062, '04/01/2022': 55589, '05/01/2022': 83744, '06/01/2022': 63076, '07/01/2022': 88358, '08/01/2022': 52544}}

I need to create a new_dict_sample with dict_sample wrapped around a list structure called, say historical, that would make it look like this:

{'historical:
[{'close': {'01/01/2022': 70327.6216, '02/01/2022': 69699.74376000001, '03/01/2022': 68400.97064, '04/01/2022': 67238.73537000001, '05/01/2022': 63901.21185000001, '06/01/2022': 63857.56575, '07/01/2022': 61243.99047, '08/01/2022': 61428.6952},
 'high': {'01/01/2022': 70592.20832, '02/01/2022': 70632.42668, '03/01/2022': 70059.4992, '04/01/2022': 69685.67126, '05/01/2022': 69189.04710000001, '06/01/2022': 64857.41691000001, '07/01/2022': 63574.54257, '08/01/2022': 62360.916040000004},
 'low': {'01/01/2022': 68087.621, '02/01/2022': 68825.25224, '03/01/2022': 67331.1328, '04/01/2022': 66893.72049, '05/01/2022': 62590.39965, '06/01/2022': 62944.24587, '07/01/2022': 60158.021, '08/01/2022': 59810.57563},
 'open': {'01/01/2022': 68087.621, '02/01/2022': 70328.65284, '03/01/2022': 69673.37348000001, '04/01/2022': 68116.52833, '05/01/2022': 67443.40665, '06/01/2022': 64352.60172, '07/01/2022': 63530.61439, '08/01/2022': 61250.77133},
 'volume': {'01/01/2022': 31239, '02/01/2022': 27020, '03/01/2022': 41062, '04/01/2022': 55589, '05/01/2022': 83744, '06/01/2022': 63076, '07/01/2022': 88358, '08/01/2022': 52544}}]}

I've tried the following, but it returns an error:

new_dict_sample = [{k : val[x] for k, val in dict_sample.items()}
    for x in range(2)]

What am I missing here? Would greatly appreciate any help!

CodePudding user response:

You can use: new_dict_sample = { 'historical': [dict_sample]};

  • Related