Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13195
| 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 | <python-python-list@m.gmane.org> |
| 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 <tjreedy@udel.edu> |
| Subject | Re: Idioms combining 'next(items)' and 'for item in items:' |
| Date | Mon, 12 Sep 2011 15:24:14 -0400 |
| References | <mailman.944.1315683474.27778.python-list@python.org> <Xns9F5E8F6211D03duncanbooth@127.0.0.1> |
| 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 | <Xns9F5E8F6211D03duncanbooth@127.0.0.1> |
| 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.1045.1315855471.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 9/12/2011 9:06 AM, Duncan Booth wrote:
> Terry Reedy<tjreedy@udel.edu> 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:
>> <set up with next(items)>
>> 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:
> <process first>
> break
I could, but I doubt I would ;-). Try...except StopIteration: pass is
more explicit and less roundabout.
> for item in items:
> <process non-first>
>
> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Idioms combining 'next(items)' and 'for item in items:' Terry Reedy <tjreedy@udel.edu> - 2011-09-10 15:36 -0400
Re: Idioms combining 'next(items)' and 'for item in items:' Duncan Booth <duncan.booth@invalid.invalid> - 2011-09-12 13:06 +0000
Re: Idioms combining 'next(items)' and 'for item in items:' Terry Reedy <tjreedy@udel.edu> - 2011-09-12 15:24 -0400
csiph-web