Reputation: 21
I have a python code that I need to re-run for each new set of dates. Each time I run it I need to change my start and end date in the code. The code looks like this:
startdate = '20150831'
enddate='20160831'
Is there a way to automate it so that the code iterates through the dates in my lists and runs the model for each set of dates?
startdate =['20150831','20150730','20150630']
enddate = ['20160831','20160730','20160630']
Upvotes: 0
Views: 164
Reputation: 2007
We can make use of the zip()
builtin function. It will combine the two lists so that it will look something like this:
>>> list(zip(startdate, enddate)) # Zip returns a zip object, so we need to cast to list to make it readable
# [('20150831', '20160831'), ('20150730', '20160730'), ('20150630', '20160630')]
And we can implement it like so
if isinstance(startdate, str): # Making sure that startdate is iterable in the way you want
startdate = [startdate]
if isinstance(enddate, str):
enddate = [enddate]
for start, end in zip(startdates, enddates):
model(start, end) # Run your model through the set
Upvotes: 1