Say I have the following list of dictionaries called sorted_bearish_trading_pairs_info
:
sorted_bearish_trading_pairs_info = [{'Trading Pair': 'TCTUSDT', '24h Change': -0.00845, '24h Volume(USDT)': 1926012.715, '0.5d ago Parkinson Volatility': 0.05589759007855682, '0.5d ago True Price Range': -0.03208065994500455, '0.5d ago Maximum Price Range': 0.02016498625114571, 'Last Close Price': 0.01056, 'Last Start Date UTC': Timestamp('2022-07-09 19:00:00')}, {'Trading Pair': 'NMRUSDT', '24h Change': 0.00605, '24h Volume(USDT)': 7340230.2529, '0.5d ago Parkinson Volatility': 0.058917395795956236, '0.5d ago True Price Range': -0.040945790080738224, '0.5d ago Maximum Price Range': 0.03402537485582467, 'Last Close Price': 16.63, 'Last Start Date UTC': Timestamp('2022-07-09 19:00:00')}, {'Trading Pair': 'STEEMUSDT', '24h Change': 0.11779999999999999, '24h Volume(USDT)': 16649505.68257, '0.5d ago Parkinson Volatility': 0.11376745797366504, '0.5d ago True Price Range': -0.05561653860226854, '0.5d ago Maximum Price Range': 0.03732162458836439, 'Last Close Price': 0.2581, 'Last Start Date UTC': Timestamp('2022-07-09 19:00:00')}, {'Trading Pair': 'WINGUSDT', '24h Change': 0.14821, '24h Volume(USDT)': 29672792.925, '0.5d ago Parkinson Volatility': 0.14523057363857397, '0.5d ago True Price Range': -0.05580029368575623, '0.5d ago Maximum Price Range': 0.08076358296622625, 'Last Close Price': 6.43, 'Last Start Date UTC': Timestamp('2022-07-09 19:00:00')}, {'Trading Pair': 'BTCSTUSDT', '24h Change': -0.16942000000000002, '24h Volume(USDT)': 34309469.117, '0.5d ago Parkinson Volatility': 0.15723835541618095, '0.5d ago True Price Range': -0.030425963488843705, '0.5d ago Maximum Price Range': 0.04462474645030439, 'Last Close Price': 9.56, 'Last Start Date UTC': Timestamp('2022-07-09 19:00:00')}]
If I execute the following line:
[x["0.5d ago Parkinson Volatility"] for x in sorted_bearish_trading_pairs_info]
I get the value of each key named "0.5d ago Parkinson Volatility"
in every dictionary in the list:
[0.05589759007855682,
0.058917395795956236,
0.11376745797366504,
0.14523057363857397,
0.15723835541618095]
If I execute the following line:
[x["Trading Pair"] for x in sorted_bearish_trading_pairs_info]
I get the value of each key named "Trading Pair"
in every dictionary in the list:
['TCTUSDT', 'NMRUSDT', 'STEEMUSDT', 'WINGUSDT', 'BTCSTUSDT']
Now, the question I have is how could I get the values of both keys so they end up being presented as the following?
[{Trading Pair': 'TCTUSDT',
'0.5d ago Parkinson Volatility': 0.05589759007855682},
{'Trading Pair': 'NMRUSDT',
'0.5d ago Parkinson Volatility': 0.058917395795956236},
{'Trading Pair': 'STEEMUSDT',
'0.5d ago Parkinson Volatility': 0.11376745797366504},
{'Trading Pair': 'WINGUSDT',
'0.5d ago Parkinson Volatility': 0.14523057363857397},
{'Trading Pair': 'BTCSTUSDT',
'0.5d ago Parkinson Volatility': 0.15723835541618095}]
CodePudding user response:
Try:
out = [
{k: d[k] for k in ("Trading Pair", "0.5d ago Parkinson Volatility")}
for d in sorted_bearish_trading_pairs_info
]
print(out)
Prints:
[
{
"Trading Pair": "TCTUSDT",
"0.5d ago Parkinson Volatility": 0.05589759007855682,
},
{
"Trading Pair": "NMRUSDT",
"0.5d ago Parkinson Volatility": 0.058917395795956236,
},
{
"Trading Pair": "STEEMUSDT",
"0.5d ago Parkinson Volatility": 0.11376745797366504,
},
{
"Trading Pair": "WINGUSDT",
"0.5d ago Parkinson Volatility": 0.14523057363857397,
},
{
"Trading Pair": "BTCSTUSDT",
"0.5d ago Parkinson Volatility": 0.15723835541618095,
},
]