Re: question about for cycle



Ant <antroy@xxxxxxxxx> wrote:

On Sep 29, 11:04 am, "fdu.xia...@xxxxxxxxx" <fdu.xia...@xxxxxxxxx>
wrote:
...
What should I do if I want the outer "for" cycle to continue or break
? If I put a "continue" or "break" in the inner cycle, it has no
effect on the outer cycle.

....

I guess to an extent it would depend on the exact situation as to
which of these is more suitable. Are there any other recommended
solutions to this?


I think the other Pythonic option you have missed is to convert the nested
for loops into a single loop by writing a generator.

def ranges(limit1, limit2):
range1, range2 = range(limit1), range(limit2)
for i in range1:
for j in range2:
yield i,j

....
for i, j in ranges(10, 10):
... whatever ...
if j==6:
break

.