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


Groups > comp.lang.python > #12931

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'anyway': 0.03; 'else:': 0.03; 'generators': 0.09; 'none:': 0.09; 'tuple': 0.09; 'def': 0.15; '"none"': 0.16; '(modulo': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'message- id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'subject: \n ': 0.16; 'subject:iterable': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'code.': 0.26; "i'm": 0.27; 'fact': 0.27; 'yield': 0.29; 'asking': 0.29; 'cc:addr:gmail.com': 0.30; 'none,': 0.30; 'subject:?': 0.31; 'error': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; "i've": 0.34; 'header:User-Agent:1': 0.34; 'checking': 0.34; 'integer': 0.34; 'object': 0.35; 'none': 0.37; 'something': 0.37; 'could': 0.38; 'signal': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'skip:d 20': 0.39; 'your': 0.61; 'custom': 0.61; 'roughly': 0.67; 'subject:are': 0.70; 'subject:you': 0.81; 'cur,': 0.84; 'no?': 0.84; 'subject:Best': 0.93
Date Wed, 07 Sep 2011 19:01:33 -0500
From Tim Chase <python.list@tim.thechases.com>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.20) Gecko/20110826 Icedove/3.1.12
MIME-Version 1.0
To python-list@python.org
Subject Re: Best way to check that you are at the beginning (the end) of an iterable?
References <264a83d7-aa43-4e36-b39e-3e67488279b6@glegroupsg2000goo.googlegroups.com> <mailman.849.1315435715.27778.python-list@python.org> <d53bfc89-b402-49a6-bd4a-baa28d708f8e@glegroupsg2000goo.googlegroups.com>
In-Reply-To <d53bfc89-b402-49a6-bd4a-baa28d708f8e@glegroupsg2000goo.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Source
X-Source-Args
X-Source-Dir
Cc Laurent <laurent.payot@gmail.com>
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.857.1315443772.27778.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315443772 news.xs4all.nl 2493 [2001:888:2000:d::a6]:60734
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12931

Show key headers only | View raw


On 09/07/11 18:22, Laurent wrote:
> Anyway I was just asking if there is something better than
> enumerate. So the answer is no? The fact that I have to create
> a tuple with an incrementing integer for something as simple
> as checking that I'm at the head just sounds awfully
> unpythonic to me.

I've made various generators that are roughly (modulo 
edge-condition & error checking) something like

  def with_prev(it):
    prev = None
    for i in it:
      yield prev, i
      i = prev

  def with_next(it):
    prev = it.next()
    for i in it:
      yield prev, i
      prev = i
    yield prev, None

which can then be used something like your original

   for cur, next in with_next(iterable):
     if next is None:
       do_something_with_last(cur)
     else:
       do_regular_stuff_with_non_last(cur)

   for prev, cur in with_prev(iterable):
     if prev is None:
       do_something_with_first(cur)
     else:
       do_something_with_others(cur)

If your iterable can return None, you could create a custom 
object to signal the non-condition:

   NO_ITEM = object()

and then use NO_ITEM in place of "None" in the above code.

-tkc

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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