Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12941

Re: Best way to check that you are at the beginning (the end) of an iterable?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.073
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'passes': 0.05; 'false,': 0.09; 'url:github': 0.09; 'def': 0.15; 'argument': 0.15; 'iterator': 0.16; 'subject:iterable': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'wed,': 0.17; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'sep': 0.23; 'pm,': 0.24; 'guess': 0.26; 'import': 0.28; 'true,': 0.29; 'message- id:@mail.gmail.com': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'subject:?': 0.31; 'it.': 0.33; 'someone': 0.34; 'checking': 0.34; 'try:': 0.34; 'received:209.85.161': 0.35; 'using': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'goes': 0.39; 'subject: (': 0.39; 'more': 0.60; 'ever': 0.65; 'chain': 0.66; 'it)': 0.67; 'subject:are': 0.70; 'subject:you': 0.81; 'consumed': 0.84; 'down:': 0.84; 'sender:addr:chris': 0.84; 'to:none': 0.93; 'subject:Best': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:cc:content-type; bh=JCaVRWn10FozLAGN/Xl7ZyjcgXAQeOlbg2XBPhGABeY=; b=S3ay00YKahU5ko5BlNFUM2N4FrczEPpqO9/n04BTsZUjjDBe6umavXoaUxc16nTter gLgaWuMZ6THWn6DJvgiCunMrfK2DoGiUMbvUSqYE3Hcheeg/oJpMLz56dAIGyWkkxUeC PogNR8ZTmNE3+L0pYobtvZAUOTs1+nAHrvXGI=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <7d4ab46b-d37e-4851-bea3-11ce040815d5@glegroupsg2000goo.googlegroups.com>
References <264a83d7-aa43-4e36-b39e-3e67488279b6@glegroupsg2000goo.googlegroups.com> <7d4ab46b-d37e-4851-bea3-11ce040815d5@glegroupsg2000goo.googlegroups.com>
Date Wed, 7 Sep 2011 19:27:09 -0700
X-Google-Sender-Auth dwf5uXO-cUUxxedFit3zrIxxALY
Subject Re: Best way to check that you are at the beginning (the end) of an iterable?
From Chris Rebert <clp2@rebertia.com>
Cc python-list@python.org
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.862.1315448832.27778.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315448832 news.xs4all.nl 2550 [2001:888:2000:d::a6]:41661
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12941

Show key headers only | View raw


On Wed, Sep 7, 2011 at 5:24 PM, Miki Tebeka <miki.tebeka@gmail.com> wrote:
> I guess enumerate is the best way to check for first argument. Note that if someone passes you the iterator as argument you have now way of checking if the consumed items from it.
>
> istail can be implemented using itertools.chain, see https://gist.github.com/1202260

For the archives, if Gist ever goes down:

from itertools import chain

def istail(it):
    '''Check if iterator has one more element. Return True/False and
    iterator.'''
    try:
        i = next(it)
    except StopIteration:
        return False, it

    try:
        j = next(it)
        return False, chain([i, j], it)
    except StopIteration:
        return True, chain([i], it)


t, it = istail(iter([]))
print t, list(it)
t, it = istail(iter([1]))
print t, list(it)
t, it = istail(iter([1, 2]))
print t, list(it)

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 14:35 -0700
  Re: Best way to check that you are at the beginning (the end) of an iterable? Cameron Simpson <cs@zip.com.au> - 2011-09-08 08:48 +1000
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 16:22 -0700
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 16:22 -0700
      Re: Best way to check that you are at the beginning (the end) of an iterable? Cameron Simpson <cs@zip.com.au> - 2011-09-08 10:23 +1000
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 17:53 -0700
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 17:53 -0700
        Re: Best way to check that you are at the beginning (the end) of an iterable? Chris Torek <nospam@torek.net> - 2011-09-08 14:21 +0000
          Re: Best way to check that you are at the beginning (the end) of an iterable? Cameron Simpson <cs@zip.com.au> - 2011-09-09 08:39 +1000
      Re: Best way to check that you are at the beginning (the end) of an iterable? Tim Chase <python.list@tim.thechases.com> - 2011-09-07 19:01 -0500
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:08 -0700
        Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:08 -0700
      Re: Best way to check that you are at the beginning (the end) of an iterable? Terry Reedy <tjreedy@udel.edu> - 2011-09-07 21:06 -0400
    Re: Best way to check that you are at the beginning (the end) of an iterable? Peter Otten <__peter__@web.de> - 2011-09-09 13:04 +0200
      Re: Best way to check that you are at the beginning (the end) of an iterable? Chris Angelico <rosuav@gmail.com> - 2011-09-09 21:30 +1000
  Re: Best way to check that you are at the beginning (the end) of an iterable? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-08 10:24 +1000
    Re: Best way to check that you are at the beginning (the end) of an iterable? Terry Reedy <tjreedy@udel.edu> - 2011-09-07 21:08 -0400
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:05 -0700
  Re: Best way to check that you are at the beginning (the end) of an iterable? Miki Tebeka <miki.tebeka@gmail.com> - 2011-09-07 17:24 -0700
    Re: Best way to check that you are at the beginning (the end) of an iterable? Laurent <laurent.payot@gmail.com> - 2011-09-07 18:06 -0700
    Re: Best way to check that you are at the beginning (the end) of an iterable? Chris Rebert <clp2@rebertia.com> - 2011-09-07 19:27 -0700

csiph-web