I have the following nested dictionary:
dictionary = {(0,'AAPL'): {0: {'v': 55478991.0,
'vw': 145.868,
'o': 146.83,
'c': 145.31,
'h': 147.2285,
'l': 145.01,
't': 1677646800000,
'n': 461613}}}
My problem is that there is one "level" too many. Essentially, I would like to assign the value of the middle dictionary to the key of the outermost dictionary, in order to achieve the following result:
dictionary = {(0,'AAPL'): {'v': 55478991.0,
'vw': 145.868,
'o': 146.83,
'c': 145.31,
'h': 147.2285,
'l': 145.01,
't': 1677646800000,
'n': 461613}}
Ultimately, I have a collection of multiple such dictionaries that I need to modify. The key of the middle dictionary is always the same, but the key of the outermost dictionary will always be different. E.g.:
{(0,'LHDX'): {0: {'v': 194946367.0,
'vw': 1.123,
'o': 1.04,
'c': 0.7901,
'h': 1.45,
'l': 0.7321,
't': 1677646800000,
'n': 373039}},
(1,'LHDX'): {0: {'v': 45745084.0,
'vw': 0.6214,
'o': 0.7133,
'c': 0.56,
'h': 0.7496,
'l': 0.5156,
't': 1677733200000,
'n': 61922}},
(2,'BBBY'): {0: {'v': 38914452.0,
'vw': 1.4392,
'o': 1.53,
'c': 1.39,
'h': 1.54,
'l': 1.39,
't': 1678078800000,
'n': 77211}}}
How can I accomplish this?
Since you have clarified your question:
dictionary = {(0,'LHDX'): {0: {'v': 194946367.0,
'vw': 1.123,
'o': 1.04,
'c': 0.7901,
'h': 1.45,
'l': 0.7321,
't': 1677646800000,
'n': 373039}},
(1,'LHDX'): {0: {'v': 45745084.0,
'vw': 0.6214,
'o': 0.7133,
'c': 0.56,
'h': 0.7496,
'l': 0.5156,
't': 1677733200000,
'n': 61922}},
(2,'BBBY'): {0: {'v': 38914452.0,
'vw': 1.4392,
'o': 1.53,
'c': 1.39,
'h': 1.54,
'l': 1.39,
't': 1678078800000,
'n': 77211}}}
You can do:
dictionary = {k:v[0] for k,v in dictionary.items()}
Output
{(0, 'LHDX'): {'v': 194946367.0,
'vw': 1.123,
'o': 1.04,
'c': 0.7901,
'h': 1.45,
'l': 0.7321,
't': 1677646800000,
'n': 373039},
(1, 'LHDX'): {'v': 45745084.0,
'vw': 0.6214,
'o': 0.7133,
'c': 0.56,
'h': 0.7496,
'l': 0.5156,
't': 1677733200000,
'n': 61922},
(2, 'BBBY'): {'v': 38914452.0,
'vw': 1.4392,
'o': 1.53,
'c': 1.39,
'h': 1.54,
'l': 1.39,
't': 1678078800000,
'n': 77211}}