My dictionary is structured as such:
stockData = {
\'AAPL\': {
\'beta\': 1.01833975315094,
\'company_name\': \'Apple\',
\'dividend\':
If I understand you question correctly, you may try as follows (I just copied the stockData
below):
stockData = {
'AAPL': {
'beta': 1.01833975315094,
'company_name': 'Apple',
'dividend': 1.9341673320912078, 'total': 300},
'GOOG': {
'beta': 1.01833975315094,
'company_name': 'Apple',
'dividend': 1.9341673320912078, 'total': 300}}
I guess this is what you meant:
def get_portfolio_value(stocks, item='total'):
portValue = 0
for v in stocks.values():
portValue += v[item]
for k in stocks.keys():
stocks[k].update({'percentage': stocks[k]['total'] / portValue})
print(portValue)
get_portfolio_value(stockData, 'total')