Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'processing.': 0.03; 'bits': 0.07; 'desired.': 0.07; 'function,': 0.07; 'processing,': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'none.': 0.09; 'optionally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; 'def': 0.15; "'',": 0.16; 'last,': 0.16; 'reedy': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'statement': 0.25; 'code.': 0.26; 'function': 0.27; 'raise': 0.28; 'loop': 0.28; 'pass': 0.29; 'elements': 0.29; 'explicitly': 0.29; 'booth': 0.30; 'break': 0.32; 'does': 0.32; 'to:addr:python-list': 0.33; 'however,': 0.34; 'header:User-Agent:1': 0.34; 'test': 0.34; 'explicit': 0.34; 'subject:next': 0.34; 'try:': 0.34; 'header:X -Complaints-To:1': 0.35; 'rather': 0.35; 'unless': 0.36; 'file': 0.36; 'issue': 0.36; 'element': 0.37; 'but': 0.37; 'could': 0.38; 'received:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'header :Mime-Version:1': 0.39; 'empty': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'processing': 0.40; 'more': 0.60; 'manner': 0.64; 'special': 0.67 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Idioms combining 'next(items)' and 'for item in items:' Date: Mon, 12 Sep 2011 15:24:14 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315855471 news.xs4all.nl 2445 [2001:888:2000:d::a6]:39196 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13195 On 9/12/2011 9:06 AM, Duncan Booth wrote: > Terry Reedy wrote: > >> The statement containing the explicit next(items) call can optionally be >> wrapped to explicitly handle the case of an empty iterable in whatever >> manner is desired. >> >> try: >> >> except StopIteration: >> raise ValueError("iterable cannot be empty") >> >> > Alternatively, if all you want is for an empty iterable to do nothing, To do nothing, just pass above. If the function does nothing, it returns None. In the fix_title function, it should return '', not None. > you could write it like this: > > items = iter(iterable) > for first in items: > > break I could, but I doubt I would ;-). Try...except StopIteration: pass is more explicit and less roundabout. > for item in items: > > > However, the issue I have with any of this pulling the first element out of > the loop is that if you want special processing for the first element you > are likely to also want it for the last, Likely? I would say occasionally. Sentences have first words; file have headers. Special processing for last items is only an issue if it *replaces* the normal processing, rather than following the normal processing. > and if it is a single item you need > to process that item with both bits of special code. I don't see how that works > unless you have all elements within the single loop and test for first/last. Like so, with tests: def first_last_special(iterable): print("\nIterable is",repr(iterable)) items = iter(iterable) try: first = next(items) except StopIteration: print('Nothing'); return print(first, 'is the first item') try: current = next(items) except StopIteration: current = first else: for item in items: print(current, 'is a middle item') current = item print(current, 'is the last item') first_last_special('') first_last_special('1') first_last_special('12') first_last_special('123') first_last_special('12345') -- Terry Jan Reedy