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 507509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:48:42+00:00 2026-05-13T06:48:42+00:00

Python’s iterators are great and all, but sometimes I really do want a C-style

  • 0

Python’s iterators are great and all, but sometimes I really do want a C-style for loop – not a foreach loop. For example, I have a start date and an end date and I want to do something for every day in that range. I can do this with a while loop, of course:

    current = start
    while current <= finish:
        do_stuff(current)
        current += timedelta(1)

This works, but it’s 3 lines instead of 1 (in C or C-based languages) and I often find myself forgetting to write the incrementing line, especially if the loop body is quite complex. Is there a more elegant and less error-prone way of doing this in Python?

  • 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-13T06:48:43+00:00Added an answer on May 13, 2026 at 6:48 am

    The elegant and Pythonic way to do it is to encapsulate the idea of a range of dates in its own generator, then use that generator in your code:

    import datetime
    
    def daterange(start, end, delta):
        """ Just like `range`, but for dates! """
        current = start
        while current < end:
            yield current
            current += delta
    
    start = datetime.datetime.now()
    end = start + datetime.timedelta(days=20)
    
    for d in daterange(start, end, datetime.timedelta(days=1)):
        print d
    

    prints:

    2009-12-22 20:12:41.245000
    2009-12-23 20:12:41.245000
    2009-12-24 20:12:41.245000
    2009-12-25 20:12:41.245000
    2009-12-26 20:12:41.245000
    2009-12-27 20:12:41.245000
    2009-12-28 20:12:41.245000
    2009-12-29 20:12:41.245000
    2009-12-30 20:12:41.245000
    2009-12-31 20:12:41.245000
    2010-01-01 20:12:41.245000
    2010-01-02 20:12:41.245000
    2010-01-03 20:12:41.245000
    2010-01-04 20:12:41.245000
    2010-01-05 20:12:41.245000
    2010-01-06 20:12:41.245000
    2010-01-07 20:12:41.245000
    2010-01-08 20:12:41.245000
    2010-01-09 20:12:41.245000
    2010-01-10 20:12:41.245000
    

    This is similar to the answer about range, except that the built-in range won’t work with datetimes, so we have to create our own, but at least we can do it just once in an encapsulated way.

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

Sidebar

Related Questions

Python's fcnt module provides a method called [flock][1] to proved file locking. It's description
Python newbie here. I was trying to troubleshoot an issue with writing a csv
Python newbie getting a bit frustrated with the csv module. At this rate, it
Python: How to export Sympy image to png? Who has any idea with this?
Python 2.7 (32-bit) Windows: We're experimenting with Python 2.7's support for themed Tkinter (
Python: How to get the sum of timedelta? Eg. I just got a lot
[python 2.6 - django 1.1.1] Hello. I'm writing a custom serializer for my django
[Python 3.1] I'm following up on this answer : class prettyfloat(float): def __repr__(self): return
Python 3.1 I am doing some calculations on a data that has missing values.
[Python 3.1] Edit: mistake in the original code. I need to print a table.

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.