Nested iteration?

Joshua Landau joshua.landau.ws at gmail.com
Tue Apr 23 17:14:09 EDT 2013


On 23 April 2013 21:49, Terry Jan Reedy <tjreedy at udel.edu> wrote:

> ri= iter(range(3))
> for i in ri:
>     for j in ri:
>         print(i,j)
> # this is somewhat deceptive as the outer loop executes just once
> 0 1
> 0 2
>
> I personally would add a 'break' after 'outer_line = next(f)', since the
> first loop is effectively done anyway at that point, and dedent the second
> for statement. I find to following clearer
>
> ri= iter(range(3))
> for i in ri:
>     break
> for j in ri:
>     print(i,j)
> # this makes it clear that the first loop executes just once
> 0 1
> 0 2
>
> I would only nest if the inner loop could terminate without exhausting the
> iterator and I wanted the outer loop to then resume.
>

Surely a normal programmer would think "next(ri, None)" rather than a loop
that just breaks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130423/1dbd5911/attachment.html>


More information about the Python-list mailing list