Conditional iteration

Paul Rubin http
Wed Dec 13 21:22:04 EST 2006


at <at at tuko.nl> writes:
> You proposal, seems nice to me but it doesn't work with Python 2.4.3, should
> it work with 2.5?
> 
> Again I am just wondering if the approach for 
> 
>         [x for c x in some_list if some_condition]
> 
> and
>         x = a if b else c
> 
> could be generalized for normal straight forward iterations:
> 
> for x in some_list if some_condition:

Sorry, I got it wrong.  Should have said:

   for x in (x for x in some_list if some_condition): 
          ...

So your example would have been:

  for x in (x for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
       ... more code ...

It should work in 2.4.



More information about the Python-list mailing list