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

  • SEARCH
  • Home
  • 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 8435813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:01:02+00:00 2026-06-10T07:01:02+00:00

Introduction I came up with a cunning solution to my problem but, not-so-cunning, it

  • 0

Introduction

I came up with a cunning solution to my problem but, not-so-cunning, it doesn’t work :-/

After hours of clicking through the debugger I think, so perhaps you can verify this, that the reason it doesn’t work is because itertools.dropwhile, after the initial declaration, is fixed – whereas I was hoping I could alter the input parameters to the predicate on every loop.

The task below attempts to pick a startdate, then an enddate that follows it, then a startdate that follows that, and an enddate after the last… and so on, so that we end up with a series of date intervals which don’t overlap. The startdates come from one list, and the enddates from another.

The following solution loops over the startdates, then over the enddates, using dropwhile to pass over the dates which are in ‘the past‘. It works perfectly on the first time through. But on the second pass the enddate gets stuck at ‘2009-12-14’. I broke out “isbefore” routine just so I could see when it gets tested, and when it doesn’t. Not sure, but I think what is happening is that the whole dropwhile test gets set in stone on the first pass, and doesn’t recompile itself on each pass? Like I’d hoped/expected.

To finish it off I was hoping to wrap it all inside a while True and exit via a StopIteration exception, thus extracting the full sequence of intervals. But it never fires. When I tried neither iterator would actually ‘next’ all the way to its end.

Questions

  1. Is that (the set-in-stone conclusion) correct as to what’s happening?
  2. Is there an short, clean, elegant way to make it behave the way I’d hoped? Do I have to write my own dropwhile which acts the way I want?

Code

import itertools
import datetime

startdates = [
    datetime.date(2009, 11, 5), datetime.date(2009, 11, 13),
    datetime.date(2009, 12, 4), datetime.date(2009, 12, 7),
    datetime.date(2009, 12, 29), datetime.date(2009, 12, 30)]

enddates = [
    datetime.date(2009, 10, 1), datetime.date(2009, 10, 2),
    datetime.date(2009, 11, 4), datetime.date(2009, 12, 14),
    datetime.date(2009, 12, 15),datetime.date(2009, 12, 30)]

enddate = datetime.date(1900, 1, 1)
startdate = datetime.date(1900, 1, 1)

def isbefore(a, b):
    return a <= b

for startdate in itertools.dropwhile(lambda date: isbefore(date, enddate), startdates):
    for enddate in itertools.dropwhile(lambda date: isbefore(date, startdate), enddates):
        print startdate, enddate
        break

Current Output

2009-11-05 2009-12-14
2009-11-13 2009-12-14
2009-12-04 2009-12-14
2009-12-07 2009-12-14

Desired Output

2009-11-05 2009-12-14
2009-12-29 2009-12-30

The more observant types will notice I asked this question yesterday which presents the same problem, but that one calls for a general solution whereas this time I’m asking specifically about the workings of dropwhile.

  • 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-06-10T07:01:04+00:00Added an answer on June 10, 2026 at 7:01 am

    Once dropwhile has dropped the items which satisfy the predicate, it yields the rest of the iterable unmodified. Using itertools.ifilterfalse in its place gives your desired output, as it will continue to process the predicate even after the initial run.

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

Sidebar

Related Questions

Introduction: I want to loop through XML files with flexible categories structure. Problem: I
Introduction I've got a problem with my extension for Chrome. It supposed to show
Introduction I'm relatively new to C++. I went through all the basic stuff and
Introduction of problem: I have two forms Home.cs and Login.cs . I have ToolStripMenuItems
Introduction Hello, I'm that typical programmer that know how to use api, but tend
I am currently studying for an exam in Introduction to algorithms, and I came
In the introduction video of Google Wave they talk about a problem with chat
Introduction: I think Nape is a relatively new engine so some of you may
Introduction I've a question about something that I think it can become a little
Introduction We all know these silly arguments objects of JavaScript functions. But why object?

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.