Oct-17-2016, 04:09 PM
I have no desire to read through all that, but it looks like you've got two values, year and month, and you're incrementing the month. You could check to see if the new value is over 12 and reset it to 1...
>>> dates = [] >>> year = 2016 >>> month = 1 >>> for i in range(20): ... dates.append('{0:04d}{1:02d}'.format(year, month)) ... month += 1 ... if month > 12: ... month = 1 ... year += 1 ... >>> dates ['201601', '201602', '201603', '201604', '201605', '201606', '201607', '201608', '201609', '201610', '201611', '201612', '201701', '201702', '201703', '201704', '201705', '201706', '201707', '201708'] >>>Then there's also the datetime module and friends in case you don't want to do that math yourself.