Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; 'val': 0.07; 'python': 0.09; 'iterate': 0.09; 'raised,': 0.09; 'def': 0.10; 'dec': 0.15; 'cleaner': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable': 0.16; 'iterable:': 0.16; 'iterator': 0.16; 'iterator.': 0.16; 'nick': 0.16; 'partitioning': 0.16; 'sequence,': 0.16; 'sequence.': 0.16; 'true:': 0.16; 'twice.': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'odd': 0.17; 'yield': 0.17; '3.2': 0.22; 'subject:skip:i 10': 0.22; 'this:': 0.23; 'seems': 0.23; 'raise': 0.24; 'header:In- Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.160.46': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'sequence': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'skip:n 10': 0.63; 'ask,': 0.84; 'drops': 0.91; 'subject:Good': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=fq4OUuMjf3+lGy0SwdgwLYIkcuA4HdubVnacW/pmdBg=; b=IrEyUt3d5qDkKpoi1iKqjRIeTmEokDsy3aXVDq+nJ8nSGpfyMMoY4PYaLi1fCrUC90 5M0fRf3QJ0kAimLedrVEhWUHQPlWw8T8xjuecKst4KCkCqbJOZV320IO9/MzWG8PrfOn 4tqogxiMzIUlM3023vuMsBsCbcwqYzfZNJwsbhTAwWs64W3TkjxsEsQYDzNJ2aH1jLg5 HgqlU2YrQOtcLoC9xydsI/HTDtlgVgNGfD7QlHM2VEF4jAShNDYODrlyJeCtY0iQY0a/ 6vuSE7qwwMO151h0GVGt9RPEPTUQ1OWBxEuC6NMmMidO17KgKcO6U9nDnmc3LbQM5Gi9 xp6w== MIME-Version: 1.0 In-Reply-To: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Date: Thu, 6 Dec 2012 00:45:05 +1100 Subject: Re: Good use for itertools.dropwhile and itertools.takewhile From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354715109 news.xs4all.nl 6863 [2001:888:2000:d::a6]:33020 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34280 On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor wrote: > > takewhile mines for gold at the start of a sequence, dropwhile drops the dross at the start of a sequence. When you're using both over the same sequence and with the same condition, it seems odd that you need to iterate over it twice. Perhaps a partitioning iterator would be cleaner - something like this: def partitionwhile(predicate, iterable): iterable = iter(iterable) while True: val = next(iterable) if not predicate(val): break yield val raise StopIteration # Signal the end of Phase 1 for val in iterable: yield val # or just "yield from iterable", I think Only the cold hard boot of reality just stomped out the spark of an idea. Once StopIteration has been raised, that's it, there's no "resuming" the iterator. Is there a way around that? Is there a clean way to say "Done for now, but next time you ask, there'll be more"? I tested it on Python 3.2 (yeah, time I upgraded, I know). ChrisA