how many nested for can we utilize?

Reedick, Andrew jr9445 at ATT.COM
Mon Aug 18 17:18:52 CEST 2008

> -----Original Message-----
> From: at [mailto:python-
> at] On Behalf Of Lie
> Sent: Monday, August 18, 2008 11:04 AM
> To: python-list at
> Subject: Re: how many nested for can we utilize?
> On Aug 17, 4:23 pm, Fredrik Lundh <fred... at> wrote:
> > Patrol Sun wrote:
> > > when I use 20 for ,"SystemError: too many statically nested blocks"
> > > When I use 100 for ,"IndentationError: too many levels of
> indentation"
> > > How to handle these errors?
> >
> > so why exactly are you trying to nest 20 or 100 for-in loops?
> >
> > </F>
> I think most (all?) algorithm that might use 20+ levels of for-in
> loops could almost always be replaced with a recursive function. Or at
> least they could be broken into smaller functions.

Recursion is slow.  There was a thread on permutations and I ran some timings on various methods of nesting loops.  Also, I do remember hitting nested for limits very quickly.  I think list comprehensions allowed for deeper nesting than straight for loops.  Anyway, the thread contains various code and solutions that should help the OP.

Cookbook is relatively decent.  5 deep, 100 iterations:
	list comprehension:  11.02
	nested for loop   :  13.16   +19%
	cookbook          :  18.85   +71%
	recursion         :  69.00  +526%
	eval              :  13.30   +20%

Start of thread:


The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA623

More information about the Python-list mailing list