question about for cycle

Duncan Booth duncan.booth at invalid.invalid
Sat Sep 29 07:39:23 EDT 2007


Ant <antroy at gmail.com> wrote:

> On Sep 29, 11:04 am, "fdu.xia... at gmail.com" <fdu.xia... at gmail.com>
> 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




More information about the Python-list mailing list