extend for loop syntax with if expr like listcomp&genexp ?

Bengt Richter bokr at oz.net
Wed Jul 13 01:44:29 EDT 2005


On Tue, 12 Jul 2005 23:07:07 -0500, Terry Hancock <hancock at anansispaceworks.com> wrote:

>On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
>> On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <sjmachin at lexicon.net> wrote:
>> >Bengt Richter wrote:
>> >>     for x in (x for x in seq if x is not None):
>> >Byzantine ...
>> Perhaps not if you wanted to enumerate the selected elements, as in
>>        for i, x in enumerate(x for x in seq if x is not None):
>
>Seems like a bug waiting to happen -- wouldn't someone using that
>idiom most likely have *meant* something like this:
>
>for i,x in enumerate(seq):
>	if x is not None:
>		print "seq[%d] = %s is not None" % (i, repr(x))
>
>?
>
>But of course that's not equivalent.  It's hard to imagine a
>use case for an enumerated loop when the object being
>iterated over is anonymous (will be lost as soon as the loop
>exits).
>
Line numbers in a listing of non-None things?
Page breaks at the right places?
Filtering out '' instead of NOne from results of a string split before creating numbered
html names for links to non-blank text elements in rendering text as html?
I dunno, seems like at least a few possibilities for something halfway sensible...

Regards,
Bengt Richter



More information about the Python-list mailing list