I have a dictionary like:
{
'cashflowStatements': [
{
'endDate': '2019',
'totalCashFromOperatingActivities': -40743000,
'capitalExpenditures': -46141000
},
{
'totalCashflowsFromInvestingActivities': 148919000,
'endDate': '2018',
'capitalExpenditures': -53753000
},
{
'totalCashflowsFromInvestingActivities': -45493000,
'endDate': '2017',
},
{
'totalCashflowsFromInvestingActivities': -19651000,
'endDate': '2016',
'capitalExpenditures': -26266000
}
]
}
I want to add the values to a variable, if they are in the dictionary.
Currently I have this solution for that:
for year in dict['cashflowStatements']:
freeCashflow = 0
if 'totalCashFromOperatingActivities' in year:
freeCashflow += year['totalCashFromOperatingActivities']
if 'capitalExpenditures' in year:
freeCashflow += year['capitalExpenditures']
Is there a better way to do this, maybe with less lines of code?