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 Has this idea come up before? Does anyone else like this idea? -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert
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
On 16/11/2007, Benji York
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
Yes, I can do that, as well as I can use the 'continue' statement, but both versions are slightly more verbose and less clear than what I propose. -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert
2007/11/16, Gustavo Carneiro
Yes, I can do that, as well as I can use the 'continue' statement, but both versions are slightly more verbose and less clear than what I propose.
The question is: is this slightly more verbosity and less clarity worth enough as to make a syntax change in the language? Personally, my answer is No. -- . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/
On 16/11/2007, Gustavo Carneiro
Yes, I can do that, as well as I can use the 'continue' statement, but both versions are slightly more verbose and less clear than what I propose.
This should go to python-ideas, I guess. (FWIW, I can see the attraction of the idea, but I don't think it's worth the cost in terms of new syntax, subtle corner cases, etc etc). Paul.
I started thinking about itertools when I saw this then I realised
that your question was about changing the syntax to produce fewer
lines of code rather than writing more effiicient code.. seemed like a
case where you could use ifilter.
//Martin
are talking about cvhanging the syntax rQuoting Gustavo Carneiro
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
Has this idea come up before? Does anyone else like this idea?
-- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert
"Gustavo Carneiro"
* Terry Reedy
"Gustavo Carneiro"
wrote in message news:a467ca4f0711160617p11564c05q8b2c59e87981c028@mail.gmail.com... |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 Why write it backwards?
for r in results: if r.numNodes == numNodes # do something with r
is the direct parallel with the below code.
The extra level of indentation is awkward. -- mithrandi, i Ainil en-Balandor, a faer Ambar
participants (7)
-
Benji York
-
Facundo Batista
-
Gustavo Carneiro
-
martin@martinthomas.net
-
Paul Moore
-
Terry Reedy
-
Tristan Seligmann