Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4382896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:50:16+00:00 2026-05-21T12:50:16+00:00

I would like to create a python function that would allow me to iterate

  • 0

I would like to create a python function that would allow me to iterate over the months from a start point to a stop point. For example it would look something like

def months(start_month, start_year, end_month, end_year):

Calling months(8, 2010, 3, 2011) would return:

((8, 2010), (9, 2010), (10, 2010), (11, 2010), (12, 2010), (1, 2011), (2, 2011), (3, 2011))

The function could just return a tuple of tuples, but I would love to see it as a generator (ie using yield).

I’ve checked the calendar python module and it doesn’t appear to provide this functionality. I could write a nasty for loop to do it easily enough, but I’m interested to see how gracefully it could be done by a pro.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:50:17+00:00Added an answer on May 21, 2026 at 12:50 pm

    The calendar works like this.

    def month_year_iter( start_month, start_year, end_month, end_year ):
        ym_start= 12*start_year + start_month - 1
        ym_end= 12*end_year + end_month - 1
        for ym in range( ym_start, ym_end ):
            y, m = divmod( ym, 12 )
            yield y, m+1
    

    All multiple-unit things work like this. Feet and Inches, Hours, Minutes and Seconds, etc., etc. The only thing that’s not this simple is months-days or months-weeks because months are irregular. Everything else is regular, and you need to work in the finest-grained units.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create the TV game-show Lingo in Python. In the game, players are
I'm still digging in python, and there are a few things that my C++
I am making a game in Python with Pyglet. I have just finished the
I am trying to optimize some python code (to speed up some matrix operations),
is there a way to create an alias for a scala keyword? in particular
Let's say there is a library function called get_pack() which returns a Pack object:
Not sure if people refer to slightly older questions but Accessing elements of python
I'm writing a database browser application with PyQt4 and because I'm new to Qt,
I've been ripping my hair out for a while now with these random numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.