On Wed, Feb 2, 2011 at 12:19 AM, Sean Carolan <scaro...@gmail.com> wrote: > I have a function that accepts four arguments, namely startmonth, > startyear, endmonth, and endyear. For example: > > startmonth = 8 > startyear = 2009 > endmonth = 1 > endyear = 2010 > > What would be the most straightforward way to create a list of > year/month pairs from start to end? I want to end up with a list of > tuples like this: > > mylist = [(2009, 8), (2009, 9), (2009, 10), (2009, 11), (2009, 12), (2010, 1)] >
This sounds somewhat like homework. If it is, that's fine, mention it, and we will help you. But we won't do your homework for you, so keep that in mind. That said, you can do this rather straightforwardly with two nested for loops and the range() or xrange(), one for the year and one for the month. The only problem here is that endmonth < startmonth in some cases, like the example you gave above. In that case, you can adjust endmonth so the range function will give the right amount of months, then correct in the final output to get the right numbers. Go and code something up, try stuff out, and come back and post what you get. We'll give you tips on how to improve on it. Hugo P.S.: There is also a solution using the itertools package that is possibly more succinct, but also less clear. You can investigate if you'd like, see the documentation of the itertools.product function. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor