<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Nested searchcursor to update the nesting data in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334992#M26175</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The probably looks pretty ugly, but I found two examples that I got working to create a list and definition query for the features.&amp;nbsp; Here's the entire script.&amp;nbsp; I did not try del rows2 yet in the first loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address") [0]
ID_Silvis = int()

field = 'NEW_PIN'
fcName = 'H2O_Pts_Nearest_Address'
myList = set([row.getValue(field) for row in arcpy.SearchCursor(fcName)])

field = 'NEW_PIN'
dquery = []
for mapLyr2 in arcpy.mapping.ListLayers(mxd, fcName):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in arcpy.SearchCursor(mapLyr2):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dquery.append("'{}'".format(row.NEW_PIN))
&amp;nbsp;&amp;nbsp;&amp;nbsp; mapLyr1.definitionQuery = '"NEW_PIN" in ({})'.format(", ".join(dquery))


rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", "\"NEW_PIN\" = '%s'" %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.SearchCursor(mapLyr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID_Silvis = row2.ID_Silvis
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(mapLyr1, hydfield, hydID, "PYTHON")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, ID_Silvis)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
mapLyr1.definitionQuery = ''

del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, row2, rows2, dbquery, field, myList, fcName, field#, hydID&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:52:10 GMT</pubDate>
    <dc:creator>GeoffOlson</dc:creator>
    <dc:date>2021-12-11T15:52:10Z</dc:date>
    <item>
      <title>Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334983#M26166</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a script that uses one layer to select features of a second layer in the first loop of the script.&amp;nbsp; When the second layer's features are selected, I have a second loop that is supposed to update the fields on the first layer in the first loop.&amp;nbsp;&amp;nbsp; Is there a specific way of using an updateCursor or setValue to affect the nesting loop?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;for row1 in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; count = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "row %d" %parcelrow
&amp;nbsp;&amp;nbsp;&amp;nbsp; parcelrow = parcelrow +1
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectcount = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; currentPIN = row1.getValue(selectField)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr1, "NEW_SELECTION", "\"%s\" = '%s'" %(selectField, currentPIN))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print currentPIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "ADD_TO_SELECTION", "\"%s\" = '%s'" %(selectField, currentPIN))
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectcount = int(arcpy.GetCount_management(mapLyr2).getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "%d points selected" %selectcount
&amp;nbsp;&amp;nbsp;&amp;nbsp; if selectcount == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr1, "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydrantfield = copytoField + "%d" %count
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "filling field " + hydrantfield
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydrID = row2.getValue(copyField)


#This setValue is supposed to update mayLyr1

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row1.setValue(hydrantfield, hydrID)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = count + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr1, "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The second loop seems to get stuck.&amp;nbsp; Shouldn't the loop only run through the selected features then continue back to the first loop?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Feb 2014 18:07:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334983#M26166</guid>
      <dc:creator>GeoffOlson</dc:creator>
      <dc:date>2014-02-20T18:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334984#M26167</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I have a script that uses one layer to select features of a second layer in the first loop of the script.&amp;nbsp; When the second layer's features are selected, I have a second loop that is supposed to update the fields on the first layer in the first loop.&amp;nbsp;&amp;nbsp; Is there a specific way of using an updateCursor or setValue to affect the nesting loop?&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for row1 in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; count = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "row %d" %parcelrow
&amp;nbsp;&amp;nbsp;&amp;nbsp; parcelrow = parcelrow +1
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectcount = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; currentPIN = row1.getValue(selectField)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr1, "NEW_SELECTION", "\"%s\" = '%s'" %(selectField, currentPIN))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print currentPIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "ADD_TO_SELECTION", "\"%s\" = '%s'" %(selectField, currentPIN))
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectcount = int(arcpy.GetCount_management(mapLyr2).getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "%d points selected" %selectcount
&amp;nbsp;&amp;nbsp;&amp;nbsp; if selectcount == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr1, "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydrantfield = copytoField + "%d" %count
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "filling field " + hydrantfield
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydrID = row2.getValue(copyField)


#This setValue is supposed to update mayLyr1

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row1.setValue(hydrantfield, hydrID)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = count + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr1, "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;The second loop seems to get stuck.&amp;nbsp; Shouldn't the loop only run through the selected features then continue back to the first loop?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You have left out crucial code or your code is not correct.&amp;nbsp; Nothing initializes rows2, so there are no rows to update.&amp;nbsp; If rows2 is initialized outside of this loop it will only run one time and then for the next row1 feature it will never update the row.&amp;nbsp; It also may have destroyed rows2 after finishing the first loop through that second cursor so that the second row1 feature would then trigger an error.&amp;nbsp; I also think the Select layer won't affect or screws up a cursor that was already populated.&amp;nbsp; You have to populate the cursor after the selection change.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, don't do this code.&amp;nbsp; This is bad coding from the get go and should be abandoned in favor of a dictionary or list replacing the second cursor.&amp;nbsp; Only load the second cursor table once to a dictionary or list and never read that table again.&amp;nbsp; Cursor disk reads kill scripts like this.&amp;nbsp; 1,000 list reads will be 1,000 times faster than 1,000 separate cursor queries especially if only one row is returned per the row2 cursor query you intended to have used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/102800-Copy-Row-fromr-spatial-Join-featues?highlight=embedded+cursors" rel="nofollow noopener noreferrer" target="_blank"&gt;Caleb Mackey's code proposal in this post&lt;/A&gt;&lt;SPAN&gt; to replace an embedded cursor loop with a dictionary for the inner cursor is much closer to what you should do.&amp;nbsp; Other forum posts dealing with dictionaries or lists should be what you look for rather than any embedded cursor routine code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It also looks like you are using the old cursor syntax, which is terribly slow.&amp;nbsp; If you have 10.1 or higher use data access style cursors only.&amp;nbsp; They are 10 times as fast.&amp;nbsp; If you can't use data access cursors that is even more of a reason to redesign this code to eliminate an embedded cursor at all costs.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:51:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334984#M26167</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T15:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334985#M26168</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;A href="http://forums.arcgis.com/threads/102800-Copy-Row-fromr-spatial-Join-featues"&gt;http://forums.arcgis.com/threads/102800-Copy-Row-fromr-spatial-Join-featues&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Use this thread as a reference for a dictionary instead of 2 loops.&amp;nbsp; Caleb Mackey provides excellent code samples.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Feb 2014 21:06:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334985#M26168</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2014-02-20T21:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334986#M26169</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you for that.&amp;nbsp; I kind of restarted what I had yesterday, and I'll past the whole thing this time.&amp;nbsp; I understand the posts I've quickly looked at in that link and will give that consideration when I rework this again.&amp;nbsp; For now this is what I have.&amp;nbsp; And just our of curiousity, I already know you don't recommend the old cursors (read that part) can a nested loop update or set a value to a layer outside of itself?&amp;nbsp; In other words, can "row2" update a field in "row1?"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os, time

#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address2") [0]
ID_Silvis = int()

rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", " \"NEW_PIN\" = '%s' " %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor(mapLyr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydID = ID_Silvis
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, hydID)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")

del mxd, row, rows1, row2, rows2, mapLyr1, mapLyr2, hydcount, hydfield, hydID&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know this code is considered slow, but from what I've learned of Python so far, it's the best I know how to do.&amp;nbsp; But I will read through the page you linked.&amp;nbsp; Thank you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:51:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334986#M26169</guid>
      <dc:creator>GeoffOlson</dc:creator>
      <dc:date>2021-12-11T15:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334987#M26170</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thank you for that.&amp;nbsp; I kind of restarted what I had yesterday, and I'll past the whole thing this time.&amp;nbsp; I understand the posts I've quickly looked at in that link and will give that consideration when I rework this again.&amp;nbsp; For now this is what I have.&amp;nbsp; And just our of curiousity, I already know you don't recommend the old cursors (read that part) can a nested loop update or set a value to a layer outside of itself?&amp;nbsp; In other words, can "row2" update a field in "row1?"&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os, time

#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address2") [0]
ID_Silvis = int()

rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", " \"NEW_PIN\" = '%s' " %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor(mapLyr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydID = ID_Silvis
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, hydID)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")

del mxd, row, rows1, row2, rows2, mapLyr1, mapLyr2, hydcount, hydfield, hydID&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;I know this code is considered slow, but from what I've learned of Python so far, it's the best I know how to do.&amp;nbsp; But I will read through the page you linked.&amp;nbsp; Thank you.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The code should work (slowly), since embedded cursors are possible.&amp;nbsp; I am not sure what ID_Silvis is doing, since it is initialized to 0 and never changes.&amp;nbsp; I would have thought you would have been doing something like this instead:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydID = row2.GetValue("ID_Silvis") # or whatever field that holds the row2 ID&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also think you would delete row2 and rows2 inside the first level loop after clearing the selection on the mapLyr2 rather than outside all of the loops, since it recreates for the next first level loop row.&amp;nbsp; Also rows1.updateRow(row) should happen outside the second loop, since you only want to save the updates after every field is updated to avoid slowing the code even more.&amp;nbsp; So I would do the code something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os, time

#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address2") [0]
ID_Silvis = int()

rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", " \"NEW_PIN\" = '%s' " %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor(mapLyr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydID = row2.GetValue("ID_Silvis") # or whatever field that holds the row2 ID
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, hydID)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; del row2, rows2

del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, hydID&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This won't void out "Hydr_Num" + str(hydcount) for any row later deleted from mapLyr2.&amp;nbsp; So if a previous run of the program assigned a value to one of these fields and then you deleted one of the hydrants from that run, the next run would not null out the now unused hHydr_Num# field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You would need a second loop after clearing the selection to continue from the last hydcount value in the rows2 loop to the last number of the Hydr_Num# fields you have created to Null out the rest of the fields (or set them to some other value indicating nothing was found).&amp;nbsp; Then you would process the rows1.updateRow(row) line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You might also consider a fail safe break in the event you reach a Hydr_Num# count that exceeds the number of fields you have created and some type of print statement to alert you that you hit it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:52:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334987#M26170</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T15:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334988#M26171</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you for your help Richard.&amp;nbsp; You're right about ID_Silvis.&amp;nbsp; Between revisions that got mixed up.&amp;nbsp; ID_Silvis is the field I need copied.&amp;nbsp; So I went and corrected that and on a single feature with 2 points it seems to be working.&amp;nbsp; The next thing, would be to create a selection of features to run the script on.&amp;nbsp; So right now I have about 340 points, but when I do a join, they only seem to be associated with 90 parcels.&amp;nbsp; So to create a definition query that could select just those 90 parcels and run the script on those instead of 3,500 parcels.&amp;nbsp; I will be reading into how to create a list and using that to create a selection.&amp;nbsp; So far here's what I've got working.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address") [0]
ID_Silvis = int()

rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", "\"NEW_PIN\" = '%s'" %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.UpdateCursor(mapLyr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID_Silvis = row2.ID_Silvis
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(mapLyr1, hydfield, hydID, "PYTHON")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, ID_Silvis)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")

del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, hydID, row2, rows2&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:52:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334988#M26171</guid>
      <dc:creator>GeoffOlson</dc:creator>
      <dc:date>2021-12-11T15:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334989#M26172</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thank you for your help Richard.&amp;nbsp; You're right about ID_Silvis.&amp;nbsp; Between revisions that got mixed up.&amp;nbsp; ID_Silvis is the field I need copied.&amp;nbsp; So I went and corrected that and on a single feature with 2 points it seems to be working.&amp;nbsp; The next thing, would be to create a selection of features to run the script on.&amp;nbsp; So right now I have about 340 points, but when I do a join, they only seem to be associated with 90 parcels.&amp;nbsp; So to create a definition query that could select just those 90 parcels and run the script on those instead of 3,500 parcels.&amp;nbsp; I will be reading into how to create a list and using that to create a selection.&amp;nbsp; So far here's what I've got working.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sounds to me like all you need to do is include a Select By Location on mapLyr1 before running the loop.&amp;nbsp; You should also change the rows2 cursor from an Update cursor to just a search cursor if you are not actually updating the row2 objects.&amp;nbsp; You could add speed by limiting the attribute fields to just the ID_Silvis field for rows2, since you are not using all of the fields.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would have thought your row update cursor would expand the field list to include all possible "Hydr_Num"# field names in the field list, since that is safer.&amp;nbsp; You should include all field names that you read from or write to in the field list.&amp;nbsp; I also think you should include the&amp;nbsp;&amp;nbsp;&amp;nbsp; del row2, rows2 line I have added, since those cursor objects regenerate in the loop and need clean up there.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address") [0]
ID_Silvis = int()

# uncomment the next line if you want to be sure all points affect the parcels selected
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
arcpy.SelectLayerByLocation_management(mapLyr1, 'intersect', mapLyr2)
# add every Hydr_Num field name to list
rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN; Hydr_Num1; Hydr_Num2; Hydr_Num3; Hydr_Num4")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", "\"NEW_PIN\" = '%s'" %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.SearchCursor(mapLyr2, "", "", "ID_Silvis")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID_Silvis = row2.ID_Silvis
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(mapLyr1, hydfield, hydID, "PYTHON")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, ID_Silvis)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; del row2, rows2
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")

del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, hydID&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:52:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334989#M26172</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T15:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334990#M26173</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Since there aren't too many points to parcels in the data, I was able to sort the attribute table of the points and see what the max number of hydrants per parcel there was and so I created 6 or 7 "hydr_x" fields.&amp;nbsp; The counter starts at one and can fill up to that point.&amp;nbsp; You're right about what if I run out of fields, though.&amp;nbsp; Then the counter resets back to 1 at the beginning of the first loop.&amp;nbsp; I tried adding the del row2, rows2 to the end of the first loop, but I get an error saying row2, rows2 isn't defined.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For a selection, I was thinking about creating a list of all unique values in the point layer and using that as a definition query.&amp;nbsp; Not all the points are contained within a parcel.&amp;nbsp; But I did use a spatial join of "nearest neighbor" to get a PIN for each point.&amp;nbsp; So select by location wouldn't return all the parcels that need to be selected.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Feb 2014 17:28:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334990#M26173</guid>
      <dc:creator>GeoffOlson</dc:creator>
      <dc:date>2014-02-24T17:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334991#M26174</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Since there aren't too many points to parcels in the data, I was able to sort the attribute table of the points and see what the max number of hydrants per parcel there was and so I created 6 or 7 "hydr_x" fields.&amp;nbsp; The counter starts at one and can fill up to that point.&amp;nbsp; You're right about what if I run out of fields, though.&amp;nbsp; Then the counter resets back to 1 at the beginning of the first loop.&amp;nbsp; I tried adding the del row2, rows2 to the end of the first loop, but I get an error saying row2, rows2 isn't defined.&lt;BR /&gt;&lt;BR /&gt;For a selection, I was thinking about creating a list of all unique values in the point layer and using that as a definition query.&amp;nbsp; Not all the points are contained within a parcel.&amp;nbsp; But I did use a spatial join of "nearest neighbor" to get a PIN for each point.&amp;nbsp; So select by location wouldn't return all the parcels that need to be selected.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Since you have defined layers you can use the Add Join tool.&amp;nbsp; Make sure both feature classes have an Attribute Index on the NEW_PIN common fields and then create the join on those fields.&amp;nbsp; Then do a select by attribute where the mapLyr2tablename.OBJECTID &amp;gt; -1.&amp;nbsp; Then use the Remove Join tool.&amp;nbsp; That will do the selection for you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just for my entertainment do just del rows2 instead of del row2 and rows2.&amp;nbsp; But if you don't have a lot of records it could be unnecessary.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Feb 2014 17:54:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334991#M26174</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-02-24T17:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334992#M26175</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The probably looks pretty ugly, but I found two examples that I got working to create a list and definition query for the features.&amp;nbsp; Here's the entire script.&amp;nbsp; I did not try del rows2 yet in the first loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address") [0]
ID_Silvis = int()

field = 'NEW_PIN'
fcName = 'H2O_Pts_Nearest_Address'
myList = set([row.getValue(field) for row in arcpy.SearchCursor(fcName)])

field = 'NEW_PIN'
dquery = []
for mapLyr2 in arcpy.mapping.ListLayers(mxd, fcName):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in arcpy.SearchCursor(mapLyr2):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dquery.append("'{}'".format(row.NEW_PIN))
&amp;nbsp;&amp;nbsp;&amp;nbsp; mapLyr1.definitionQuery = '"NEW_PIN" in ({})'.format(", ".join(dquery))


rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", "\"NEW_PIN\" = '%s'" %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.SearchCursor(mapLyr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID_Silvis = row2.ID_Silvis
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(mapLyr1, hydfield, hydID, "PYTHON")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, ID_Silvis)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
mapLyr1.definitionQuery = ''

del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, row2, rows2, dbquery, field, myList, fcName, field#, hydID&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:52:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334992#M26175</guid>
      <dc:creator>GeoffOlson</dc:creator>
      <dc:date>2021-12-11T15:52:10Z</dc:date>
    </item>
    <item>
      <title>Re: Nested searchcursor to update the nesting data</title>
      <link>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334993#M26176</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The probably looks pretty ugly, but I found two examples that I got working to create a list and definition query for the features.&amp;nbsp; Here's the entire script.&amp;nbsp; I did not try del rows2 yet in the first loop.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "Parcels_With_PageNum") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "H2O_Pts_Nearest_Address") [0]
ID_Silvis = int()

field = 'NEW_PIN'
fcName = 'H2O_Pts_Nearest_Address'
myList = set([row.getValue(field) for row in arcpy.SearchCursor(fcName)])

field = 'NEW_PIN'
dquery = []
for mapLyr2 in arcpy.mapping.ListLayers(mxd, fcName):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in arcpy.SearchCursor(mapLyr2):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dquery.append("'{}'".format(row.NEW_PIN))
&amp;nbsp;&amp;nbsp;&amp;nbsp; mapLyr1.definitionQuery = '"NEW_PIN" in ({})'.format(", ".join(dquery))


rows1 = arcpy.UpdateCursor(mapLyr1, "", "", "NEW_PIN")
for row in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; newpin = row.NEW_PIN
&amp;nbsp;&amp;nbsp;&amp;nbsp; print newpin
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "NEW_SELECTION", "\"NEW_PIN\" = '%s'" %newpin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.SearchCursor(mapLyr2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydfield = "Hydr_Num" + str(hydcount)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID_Silvis = row2.ID_Silvis
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(mapLyr1, hydfield, hydID, "PYTHON")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(hydfield, ID_Silvis)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hydcount = hydcount + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows1.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
mapLyr1.definitionQuery = ''

del mxd, row, rows1, mapLyr1, mapLyr2, hydcount, hydfield, row2, rows2, dbquery, field, myList, fcName, field#, hydID&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The code addition does not look all that bad and using a definition query is wise, since they are faster than regular queries.&amp;nbsp; Anyway, if the code now does everything you need that is great.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, now that you are running through the entire record set of the mapLay2 anyway before entering your first loop why build a list and not a dictionary?&amp;nbsp; The dictionary would replace the entire inside loop and your performance would increase at least 10 fold minimum if done correctly.&amp;nbsp; Hitting the recordset on disk for each inner loop query really makes perfance go down the tubes if done over a big enough record set.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You would build the dictionary much like you have the list with the NEW_PIN values acting as your key.&amp;nbsp; A tuple object would hold all of the hydrant numbers as the dictionary value associated each key.&amp;nbsp; Iterating the dictionary keys to create your definition query is no more time consuming that iterating the list.&amp;nbsp; The tuples of hydrant numbers within each key would iterate at blazing speed compared to the code you have written.&amp;nbsp; As long as you are not talking incredibly large datasets, load your entire mapLyr2 table to memory before entering the first loop and this tool should perform like a dream.&amp;nbsp; You can eliminate the Search Layer by Attribute on the mapLyr2 (big speed gain) and the inner rows2 cursor with an iteration of the tuple value associated with the NEW_PIN key.&amp;nbsp; You are now 75% there in your code structure.&amp;nbsp; When you go to data access cursors ultimately after upgrading to 10.1 or higher you would get another big speed boost.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:52:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-searchcursor-to-update-the-nesting-data/m-p/334993#M26176</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T15:52:13Z</dc:date>
    </item>
  </channel>
</rss>

