[Python-ideas] if-statement in for-loop

Daniel Moisset dmoisset at machinalis.com
Tue Oct 4 08:03:05 EDT 2016


Something else that may look confusing can be a break statement; in a

for i in range(5) for j in range(5) for k in range(5):
   ...
   break

does it break the inner "k" loop, going to the next "j" (as it would happen
with 3 nested loops), or does it end the whole for statement? Similar
question with "continue"


On 4 October 2016 at 12:37, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 4 October 2016 at 14:32, Ken Kundert <admin at shalmirane.com> wrote:
> > For example, it was suggested that one could simplify a multi-level loop
> by
> > moving the multiple levels of for loop into a separate function that
> acts as
> > generator. And that is a nice idea, but when writing it, the writing of
> the
> > generator function represents a speed bump. Whereas writing something
> like the
> > following is simple, compact, quick, and obvious. There is no reason why
> it
> > should not be allowed even though it might not always be the best
> approach to
> > use:
> >
> >     for i in range(5) for j in range(5) for k in range(5):
> >         ...
> >
> > And I would really like to be able to write loops of the form:
> >
> >     for item in items if item is not None:
> >        ...
> >
> > It is something I do all the time, and it would be nice if it did not
> consume
> > two levels on indentation.
>
> And when you add the "else" clause that's supported by both "for" and
> "if", what does that mean in the abbreviated form?
>
>     for item in items if item is not None:
>         ...
>     else:
>         # ???
>
> Or is the implicit proposal that this form be special cased to
> disallow the "else" clause?
>
> Comprehensions don't have that concern, as they don't support "else"
> clauses at all.
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Daniel F. Moisset - UK Country Manager
www.machinalis.com
Skype: @dmoisset
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161004/f4848476/attachment-0001.html>


More information about the Python-ideas mailing list