There have been similar questions but I’ve not quite been able to get it right – apologies for duplications. What I would like to do is create dataframe names inside a dictionary based on variables as I iterate.
d = {} market = 'SPX' datatype = 'prices' for j in range(10): year = 2010+j start = str(year) + '-01-01' end = str(year+2) + '-01-01' d['{market}_{datatype}_from_{start}_to{end}'] = 'foo'
I would like for the elements of the dictionary to be called
spx_prices_from_2010-01-01_to_2012-01-01 spx_prices_from_2011-01-01_to_2013-01-01 ...
Thank you very much in advance
Answer
If you’re using Python 3, you can use f-strings:
d = {} market = 'SPX' datatype = 'prices' for j in range(10): year = 2010+j start = str(year) + '-01-01' end = str(year+2) + '-01-01' d[f"{market}_{datatype}_from_{start}_to_{end}"] = 'foo'
Alternatively, you can use str
‘s format
method.