# Arcpy Map Algebra: Maximum Number of Nestings

When using Map Algebra you can nest multiple expressions as can be seen in the Esri example below. Is there a limit on how many long of a statement you can have OR a limit on how many Con statements you can nest? If there is no limit, is there a best practice?

``````OutRas = Con(InRas1 > 23, 5, Con(InRas1 > 20, 12, Con((InRas1 > 2) & (InRas1 < 17), Sin(InRas1), 100)))
``````
• Do you think you've found a limit? Is there a specific problem you are having with it? How many have you tried? – Midavalo Apr 10 '17 at 19:54
• I have used 3 nested con statements and have a formula that starts with 8 parentheses to give you an idea on how deep it goes and these seem have successful results. – Rex Apr 10 '17 at 20:13
• Looking at the example it looks like you'd be better off using ReclassifyByTable resources.arcgis.com/en/help/main/10.2/index.html#//… instead of nested Con statements. For each 'sub' Con a new raster object has to be made using resources (RAM and/or disc), so there's a resource limit, and time for each. I think it would be much faster to run a single reclass, more stable and less resource hungry. What are you trying to achieve with these nested statements? If it's working then what is the problem you're having? or are you just curious to know how far you can push it? – Michael Stimson Apr 10 '17 at 22:59
• I have no problem so far, but I haven’t added everything I want yet. I have several large branching formulas with trig functions that involve several rasters and tests against others rasters. – Rex Apr 11 '17 at 21:46
• If it uses resources to create a raster anyways, I might just break the steps up into separate in_memory rasters and use python conditionals rather than all in one statement. That way it will be easier to follow and I could easily make those rasters permanent should I need down the road. – Rex Apr 11 '17 at 21:46

I haven't found a limit in the number of nestings in a Map Algebra expression, however there is a limit to the length of the expression itself:

The maximum length of the logical expression is 4,096 characters

My guess is that if you hit a limit to the number of nestings in an expression, it will be based on your system resources and not on any hard limit in the Map Algebra tools. I don't have any evidence to back this theory however.

I've hit two limits while working on one of my projects.

I was creating a raster product with several layers of conditional statements. The first was the character limit mentioned by @Midavalo.The following is a simplified version of what was returned to me:

``````Traceback (most recent call last):   File "C:\Users\SDL\Desktop\map_maker_f.py", line 15, in <module>
Raster('L813188i.tif')==5,Con(Raster('edist.tif')> 12.1655,Con(Raster(
...
1e-005*Raster('rad_031m.tif'))))) TypeError: Con() takes at most 4 arguments (9 given)
``````

ArcGIS Pro gives the following line:

``````TypeError: Con() takes from 2 to 4 positional arguments but 9 were given
``````

I made it simpler to see if it was four `Con(test,arg,arg)`s and it seems that we can only nest four of them like the following.

``````Con(test,arg,Con(test,arg,Con(test,arg,Con(test,arg,arg))))
``````

The workaround I'm using is to break my cons and mosaic the results.

``````Con(test,arg,Con(test,arg,Con(test,arg,Con(test,arg)))) + [mosaic]
Con(test,arg,Con(test,arg,Con(test,arg,Con(test,arg,arg))))
``````