For loop comprehensions
joncle at googlemail.com
Fri Feb 11 18:44:01 EST 2011
On Feb 11, 11:10 pm, Benjamin S Wolf <bsw... at google.com> wrote:
> It occurred to me as I was writing a for loop that I would like to
> write it in generator comprehension syntax, eg.
> for a in b if c:
> rather than using one of the more verbose but allowable syntaxes:
> for a in (x for x in b if c):
> for a in b:
> if not c: continue
> Python 3.1 does not support "for comprehensions", and a few cursory
> searches of PEPs and this list don't turn up anything. I like the idea
> enough to suggest it though I'm unfamiliar with the PEP/feature
> request process (PEP 1 pointed me here). What do other people think?
Can't help with the PEP stuff, but ummm... is anything wrong with
filter()? For some things, I personally find it more readable in some
circumstances than list/gen comps. I don't mind predicates at "the
end", but sometimes they're more obvious at "the front".
More information about the Python-list