List-comprehension-like extensions in normal for loops

How about letting ordinary for loops contain multiple "for"s and optionally "if"s like in list comprehensions and generator expressions? For example, on a site I was just looking at, it had the line: for record in [ r for r in db if r.name == 'pierre' ]: which could instead be: for record in db if record.name == 'pierre': And of course there could be much more complex ones too. I know you could just do "for x in <some generator expression>" and get the same effect and roughly the same speed, but I think this looks a lot nicer, and it would make sense to have this sort of consistency across the multiple contexts in which "for" can be used.

How about just nesting the for's/if's? for record in db: if record.name == 'pierre': ... Isn't that the one obvious way to do it? On Sun, Mar 2, 2008 at 12:24 AM, Adam Atlas <adam@atlas.st> wrote:
How about letting ordinary for loops contain multiple "for"s and optionally "if"s like in list comprehensions and generator expressions? For example, on a site I was just looking at, it had the line: for record in [ r for r in db if r.name == 'pierre' ]: which could instead be: for record in db if record.name == 'pierre':
And of course there could be much more complex ones too. I know you could just do "for x in <some generator expression>" and get the same effect and roughly the same speed, but I think this looks a lot nicer, and it would make sense to have this sort of consistency across the multiple contexts in which "for" can be used. _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

Or even... for record in (r for r in db record.name == 'pierre'): ... Hello generator expressions (available since Python 2.4). But I'm with Eyal, personally. - Josiah On Sat, Mar 1, 2008 at 2:57 PM, Eyal Lotem <eyal.lotem@gmail.com> wrote:
How about just nesting the for's/if's?
for record in db: if record.name == 'pierre': ...
Isn't that the one obvious way to do it?
On Sun, Mar 2, 2008 at 12:24 AM, Adam Atlas <adam@atlas.st> wrote:
How about letting ordinary for loops contain multiple "for"s and optionally "if"s like in list comprehensions and generator expressions? For example, on a site I was just looking at, it had the line: for record in [ r for r in db if r.name == 'pierre' ]: which could instead be: for record in db if record.name == 'pierre':
And of course there could be much more complex ones too. I know you could just do "for x in <some generator expression>" and get the same effect and roughly the same speed, but I think this looks a lot nicer, and it would make sense to have this sort of consistency across the multiple contexts in which "for" can be used. _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
participants (3)
-
Adam Atlas
-
Eyal Lotem
-
Josiah Carlson