Iterating over several lists at once
Mike Erickson
mee at quidquam.com
Wed Dec 13 15:14:48 EST 2006
* at (at at tuko.nl) wrote:
> Sorry for breaking into this thread, but I agree completely that any
> unnecessary indentations should be avoided. For the same reason I advocate
> that the following syntax should work:
>
> for x in some_list if some_condition:
> ... code ...
>
> in stead of
>
> for x in some_list
> if some_condition:
> ... code ...
It is possible to avoid the extra level of indentaion, but I think it's
much less readable than the 2-level verbose expresion:
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> for odd in (num for num in a if num % 2 == 1):
... print odd
...
1
3
5
7
there is also continue, which I think is a good compromise:
>>> for num in a:
... if num % 2 == 0:
... continue
... print num
...
1
3
5
7
HTH (and not lead astray),
mike
More information about the Python-list
mailing list