Conditional iteration

Chris Mellon arkanes at gmail.com
Wed Dec 13 10:52:04 EST 2006


On 13 Dec 2006 07:47:23 -0800, Paul Rubin
<"http://phr.cx"@nospam.invalid> wrote:
> at <at at tuko.nl> writes:
> > I have a lot times the following code:
> >
> > for x in [-2, -1, 0, 1, 2, 3, 4]:
> >         if x > 0:
> >                 ... more code...
>
> Use:
>
>  for x in (x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
>       ... more code ...
> --

or filter:

from itertools import ifilter

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



> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list