My dictionary is structured as such:
stockData = {
\'AAPL\': {
\'beta\': 1.01833975315094,
\'company_name\': \'Apple\',
\'dividend\':
This is what you want:
stock_data = {
'AAPL': {'beta': 1.01833975315094, 'company_name': 'Apple', 'dividend': 1.9341673320912078, 'total':300},
'GOOG': {'beta': 1.01833975315094, 'company_name': 'Apple', 'dividend': 1.9341673320912078, 'total':300}
}
stock_sum = sum(stock_data[item]['total'] for item in stock_data)
for item in stock_data:
stock_data[item]['percentage'] = int((float(stock_data[item]['total']) / stock_sum) * 100)
Result:
>>> for item in stock_data:
... print stock_data[item]['percentage']
...
50
50