Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > if statement in for loop

Reply
Thread Tools

if statement in for loop

 
 
Ryan Lowe
Guest
Posts: n/a
 
      08-20-2003
i thought id ask here before wirting a PEP, if people thought it would be a
good enhancement to allow if clauses in regular for-statements like so:

>>> for x in y if x < 10 :


is semantically equivalent to

>>> for x in [x for x in y if x < 10] :


but is shorter and easier to read. basically, the if works as a filter for
the values in the iterator. its not a major change, purely syntactic sugar.
and clearly backwards-compatible.

seem reasonable?

ryan


 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      08-20-2003
Ryan Lowe wrote:
>
> i thought id ask here before wirting a PEP, if people thought it would be a
> good enhancement to allow if clauses in regular for-statements like so:
>
> >>> for x in y if x < 10 :

>
> is semantically equivalent to
>
> >>> for x in [x for x in y if x < 10] :

>
> but is shorter and easier to read. basically, the if works as a filter for
> the values in the iterator. its not a major change, purely syntactic sugar.
> and clearly backwards-compatible.
>
> seem reasonable?


Seems unnecessary:

for x in y:
if x < 10:
continue

blah blah...


-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments