[Python-Dev] for loop with if filter (original) (raw)
Benji York benji at benjiyork.com
Fri Nov 16 15:26:06 CET 2007
- Previous message: [Python-Dev] for loop with if filter
- Next message: [Python-Dev] for loop with if filter
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Gustavo Carneiro wrote:
I am finding myself often doing for loops over a subset of a list, like:
for r in results: if r.numNodes != numNodes: continue # do something with r
It would be nice if the plain for loop was as flexible as list comprehensions and allowed an optional if clause, like this:
for r in results if r.numNodes == numNodes: # do something with r
You can do the same today, sans sugar:
for r in (s for s in results if s.numNodes == numNodes):
# do something with r
-- Benji York http://benjiyork.com
- Previous message: [Python-Dev] for loop with if filter
- Next message: [Python-Dev] for loop with if filter
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]