My dictionary is structured as such:
stockData = {
\'AAPL\': {
\'beta\': 1.01833975315094,
\'company_name\': \'Apple\',
\'dividend\':
Your problem can be solved in two parts:
Take the sum of total stocks. For that you may use sum() as:
stock_sum = sum(stock_data['total'] for stock_data in stockData.values())
Iterate over the value to update the entry. Since you do not need key
, use dict.values()
to iterate over just the values:
for stock_data in stockData.values():
stock_data['percentage'] = stock_data['total']/stock_sum
# ^ Adds new key into your existing `dict` object
Now the values of your stockData
dict holds additional key as percentage