Reputation: 87
I've got a list of dictionaries:
[({'symbol':symbol, 'name':name, 'shares_total':shares_total, 'price':price, 'holding_value':holding_value})
({'symbol':symbol, 'name':name, 'shares_total':shares_total, 'price':price, 'holding_value':holding_value})
etc.
]
I want to introduce a variable holdings_total
which is the sum of values for all "holding_value"
keys in a list of dictionaries.
I believe there is some succinct method, but cannot figure it out.
Could someone help?
Thanks!
Upvotes: 2
Views: 3396
Reputation: 28838
#lst = list of dicts
sum_hold = sum(d.get('holding_value', 0) for d in lst)
In case the dict does not have the holding_value
key
Upvotes: 6