Select to view content in your preferred language

Nested Cursor- why is it bad?

3557
3
Jump to solution
04-21-2013 05:07 PM
by Anonymous User
Not applicable
Original User: luke.kaim

Hi Everyone,

I have heard nested cursors are bad. Why is that? If I want to test if a point is in a particular polygon wouldn't I need nested cursors?
Something like this would work well. Why is nested cursors not a good idea? Thank you for your help

for row1 in arcpy.SearchCursor(shapefile1):     for row2 in arcpy.SearchCursor(shapefile2):         if row1.shape.contains(row2.shape):             return True


Luke Kaim
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
I did not rule out a spatial join. When I solved this issue with tables I did use a spatial join. But joins do take a long time and I thought a cursor could be faster. Now I want to solve this issue programmatically. Why are nested cursors a bad idea?


I don't think there's a problem with nested cursors, as long as the datasets accessed inside the loops don't participate in the cursors. A cursor opens a file lock on the table you opened, so if you run a tool that accesses that dataset you are accessing it twice at the same time -- this is what can get you into trouble.  Your example does not do this.

Feel free to try this, but if there's a tool that will do the same thing, it's unlikely a cursor would be faster because most standard tools are written in C++, which will usually be much faster than Python.

In my experience, the most effective way to speed up a really slow geoprocessing workflow is to try a different approach that more efficiently solves your problem.

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
you ruled out a spatial join?
0 Kudos
by Anonymous User
Not applicable
Original User: luke.kaim

I did not rule out a spatial join. When I solved this issue with tables I did use a spatial join. But joins do take a long time and I thought a cursor could be faster. Now I want to solve this issue programmatically. Why are nested cursors a bad idea?
0 Kudos
curtvprice
MVP Alum
I did not rule out a spatial join. When I solved this issue with tables I did use a spatial join. But joins do take a long time and I thought a cursor could be faster. Now I want to solve this issue programmatically. Why are nested cursors a bad idea?


I don't think there's a problem with nested cursors, as long as the datasets accessed inside the loops don't participate in the cursors. A cursor opens a file lock on the table you opened, so if you run a tool that accesses that dataset you are accessing it twice at the same time -- this is what can get you into trouble.  Your example does not do this.

Feel free to try this, but if there's a tool that will do the same thing, it's unlikely a cursor would be faster because most standard tools are written in C++, which will usually be much faster than Python.

In my experience, the most effective way to speed up a really slow geoprocessing workflow is to try a different approach that more efficiently solves your problem.
0 Kudos