Let's 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')}]
Yes I run the following line:
[x["0.5d ago Parkinson Volatility"] for x in sorted_bearish_trading_pairs_info]
I get the value of each called key "0.5d ago Parkinson Volatility"
from all the dictionaries in the list:
[0.05589759007855682,
0.058917395795956236,
0.11376745797366504,
0.14523057363857397,
0.15723835541618095]
Yes I run the following line:
[x["Trading Pair"] for x in sorted_bearish_trading_pairs_info]
I get the value of each called key "Trading Pair"
from all the dictionaries in the list:
['TCTUSDT', 'NMRUSDT', 'STEEMUSDT', 'WINGUSDT', 'BTCSTUSDT']
Now, the question I have is how could I get the values of both keys so that they end up being presented as the following list of dictionaries?
[{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}]
A solution:
The
for
iterates through the dictionaries in the original list. For each dictionary, a new one is created with the two keys of interest and their respective values.This dictionary is added to the output list.
show
produces:
This is a version of Candid Moe's answer that uses a function to avoid writing repetitive and unclear code:
The extract_items function takes a first argument that would be the dictionary, and infinite other arguments that would be the keys to be accessed, iterates through the keys, creating a new dictionary where the item is "copied" from the original dictionary.