Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20806
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty |
| Followup-To | comp.lang.python |
| Date | Fri, 24 Feb 2012 13:44:15 +0100 |
| Organization | None |
| Lines | 50 |
| Message-ID | <ji80lm$rtc$1@solani.org> (permalink) |
| References | <14ba4b39-4066-4d24-89d7-3c7c85aab85c@b18g2000vbz.googlegroups.com> <51a08251-1e6e-45f3-8531-b53e87b7db8b@r1g2000yqk.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | solani.org 1330087414 28588 eJwFwYEBwDAEBMCVgvcYJyr2H6F3bhR+ATrh6ztFVuYU1O5reiVlRAFab9znoicPV4No9g8FORAo (24 Feb 2012 12:43:34 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Fri, 24 Feb 2012 12:43:34 +0000 (UTC) |
| X-User-ID | eJwNyrkBACAIBLCVQDiecVBk/xG0SxGIsR1XgykGs8NLwISiFvLOKE3OIO21M0WzZv+70j4Cty+HyxydDsI8M74VGw== |
| Cancel-Lock | sha1:Gj5A53I75ITIhBnTTlG9qGwzldw= |
| X-NNTP-Posting-Host | eJwFwYEBADAEA7CbjNacw/D/CUugFD43gobFUs5EXelQE06mbILR85aN8PSqLUyBeqP7AyQ5Edc= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:20806 |
Followups directed to: comp.lang.python
Show key headers only | View raw
Rick Johnson wrote: > On Feb 23, 6:30 pm, Alex Willmer <a...@moreati.org.uk> wrote: >> [...] >> as a standard looping-with-index construct. In Python for loops don't >> create a scope, so the loop variables are available afterward. I've >> sometimes used this to print or return a record count e.g. >> >> for i, x in enumerate(seq): >> # do stuff >> print 'Processed %i records' % i+1 > > You could employ the "else clause" of "for loops" to your advantage; >>>> for x in []: > ... print x > ... else: > ... print 'Empty Iterable' > Empty Iterable > >>>> for i,o in enumerate([]): > ... print i, o > ... else: > ... print 'Empty Iterable' > Empty Iterable No: >>> for i in []: ... pass ... else: ... print "else" ... else >>> for i in [42]: ... pass ... else: ... print "else" ... else >>> for i in [42]: ... break ... else: ... print "else" ... >>> The code in the else suite executes only when the for loop is left via break. A non-empty iterable is required but not sufficient.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Alex Willmer <alex@moreati.org.uk> - 2012-02-23 16:30 -0800
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-24 01:08 +0000
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Ethan Furman <ethan@stoneleaf.us> - 2012-02-23 19:49 -0800
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-24 07:10 +0000
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 09:44 +0100
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Paul Rubin <no.email@nospam.invalid> - 2012-02-23 17:21 -0800
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-24 04:32 -0800
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 13:44 +0100
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 14:14 +0100
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-24 14:54 +0000
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Arnaud Delobelle <arnodel@gmail.com> - 2012-02-24 15:00 +0000
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Peter Otten <__peter__@web.de> - 2012-02-24 16:16 +0100
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-28 14:56 -0800
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Chris Angelico <rosuav@gmail.com> - 2012-02-29 10:24 +1100
Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-29 00:18 +0000
csiph-web