Nested loop limit?

Peter Hansen peter at engcorp.com
Fri Jul 9 22:15:55 CEST 2004


Dan Bishop wrote:

> Peter Hansen <peter at engcorp.com> wrote:
>>chad wrote:
>>>Does Python have a limit on the number of nested for-loops?
>>
>>[Yes. 20]
> 
> However, the code:
[snip]
> doesn't break until i=227.  So if you need more than 20 nested loops,
> try replacing them with list comprehensions.

Actually, I think what both our code samples show is that if
one needs such a large number of statically nested *anything*,
the design is probably broken and should be replaced, if nothing
else, with something that builds the code on the fly...

If you consider that statically nested for loops must in some
way represent different dimensions, is it really possible that
a problem can have more than 20 dimensions (or even nearly that
many!) which must all be looped over simultaneously?  I would
try to step way back from my problem and reconsider what I'm
doing if I were ever on my way to that situation...

But I'd be interested in any ("real world", as usual) example
where it is true....

-Peter



More information about the Python-list mailing list