<?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 for loops not iterating correctly with arcpy.da.SearchCursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354064#M27825</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try the generator and cursor approach used in this sample:&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.arcgis.com/home/item.html?id=da1540fb59d84b7cb02627856f65a98d" title="http://www.arcgis.com/home/item.html?id=da1540fb59d84b7cb02627856f65a98d"&gt;http://www.arcgis.com/home/item.html?id=da1540fb59d84b7cb02627856f65a98d&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 25 Aug 2014 22:07:45 GMT</pubDate>
    <dc:creator>BruceHarold</dc:creator>
    <dc:date>2014-08-25T22:07:45Z</dc:date>
    <item>
      <title>Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354063#M27824</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am attempting to update a feature class with records from a table.&amp;nbsp; I'm doing this by listing all fields in the table, creating arcpy.da.SearchCursor objects from that list, then iterating through the rows of both the table and feature class, find when the ID fields match, then update the fields in the feature class row with values from the table row which share the same field name.&amp;nbsp; However, I have discovered that the code (below) is only iterating through one value of the outermost for loop.&amp;nbsp; I have only included the portion of code that is causing a problem.&amp;nbsp; As you can see, the code only shows the mechanics of the loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14090027938161682" jivemacro_uid="_14090027938161682" modifiedtitle="true"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;arcpy.env.workspace = "path to file geodatabase"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;points = "point_fc" #point feature class in geodatabase&lt;/P&gt;
&lt;P&gt;table = "table" #table in geodatabase&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;fields = [f.name for f in sorted(arcpy.ListFields(table))]&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;id_index = fields.index('ID')&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;pcursor = arcpy.da.SearchCursor(points,fields)&lt;/P&gt;
&lt;P&gt;tcursor = arcpy.da.SearchCursor(table,fields)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;for prow in pcursor:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for trow in tcursor:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print prow[id_index],trow[id_index]&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I run this code, it iterates through all ID values of trow (from the table), but only one of prow (from the point feature class), then stops.&amp;nbsp; Any suggestions?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Aug 2014 21:51:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354063#M27824</guid>
      <dc:creator>BenColeman1</dc:creator>
      <dc:date>2014-08-25T21:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354064#M27825</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try the generator and cursor approach used in this sample:&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.arcgis.com/home/item.html?id=da1540fb59d84b7cb02627856f65a98d" title="http://www.arcgis.com/home/item.html?id=da1540fb59d84b7cb02627856f65a98d"&gt;http://www.arcgis.com/home/item.html?id=da1540fb59d84b7cb02627856f65a98d&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Aug 2014 22:07:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354064#M27825</guid>
      <dc:creator>BruceHarold</dc:creator>
      <dc:date>2014-08-25T22:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354065#M27826</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use this code to populate a dictionary with your fields and transfer them to an Update Cursor.&amp;nbsp; The look up of the dictionary will be at least 100 times faster than any embedded cursor code, since it places the look up in memory rather than hitting the hard disk like a cursor does.&amp;nbsp; So there are only two cursor reads straight through 2 tables/feature classes using this code, rather than an exponential growth curve of loops through the dual cursors.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Substitute you table/feature class to be read, your table/feature class to be updated. you field list for the table to be read with the matching keyfield listed first, and a second field list for the table to be updated if the field names are different.&amp;nbsp; This assumes a 1 to 1 match in the fields being read and the fields being updated.&amp;nbsp; If every field in the table is matched by a field of the same name and type in the feature class you could extract a field list and just make sure the key matching field is the first in the list.&amp;nbsp; Sorting the field list is not necessary.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14090075187534586" jivemacro_uid="_14090075187534586" modifiedtitle="true"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;arcpy.env.workspace = "path to file geodatatbase"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;sourceFC = "table"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;updateFC = "points_fc"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;sourceFieldsList = ["ID", "FIELD1", "FIELD2"]&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;updateFieldsList = sourceFieldsList&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for updateRow in updateRows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keyValue = updateRow[0]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if keyValue in valueDict:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for n in range (1,len(sourceFieldsList)):&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updateRow&lt;N&gt; = valueDict[keyValue][n-1]&lt;/N&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updateRows.updateRow(updateRow)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;del valueDict&lt;/P&gt;

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Aug 2014 23:24:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354065#M27826</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-08-25T23:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354066#M27827</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your code is doing what you designed.&amp;nbsp; The reason the code never prints for any but the first prow is that the trow cursor is created outside the loop and can only be read through once.&amp;nbsp; By the time the second row of prow occurs trow is fully read and stops the code, since its index is Null.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is why embedded cursors should never be used.&amp;nbsp; If you truly embed the trow cursor so that it regenerates within the prow cursor loop you are creating an exponential growth curve of loops.&amp;nbsp; The processing time can easily become days long if there are enough records.&amp;nbsp; Even a very modest 1,000x1,000 record pairings set results in as many as 1,000,000 record reads.&amp;nbsp; My alternate code would always read just 2,000 records for a 1,000x1,000 record pairing set. That is 500 times fewer record reads.&amp;nbsp; The time it takes to do pair matching in memory is negligible compared to even one cursor read through the two tables, so cursor reads are to be reduced as much as possible and memory use should be optimized as much as possible.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unless you are processing crazy numbers of records the above code should work.&amp;nbsp; But no matter what, it will always far outperform a dual cursor embedded loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My thanks go to Chris Synder who originally introduced me to this code design.&amp;nbsp; You should always be on the look out for his posts here on the forum if you want to know many of the best Python code designs.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Aug 2014 23:42:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354066#M27827</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-08-25T23:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354067#M27828</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the example &lt;A href="https://community.esri.com/migrated-users/4811"&gt;Richard Fairhurst&lt;/A&gt;‌.&amp;nbsp; I have tried to employ your (Chris's) solution with the code below:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14092556064876372 jive_text_macro" jivemacro_uid="_14092556064876372"&gt;
&lt;P&gt;import arcpy&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;arcpy.env.workspace = "G:/Personal-Ben/Projects/Road Problems/Road Problems.gdb"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;sourceFC = "road_table"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;updateFC = "all_roads_points"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;sourceFieldsList = [f.name for f in arcpy.ListFields(sourceFC)]&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;#Identify the index for the key value&lt;/P&gt;
&lt;P&gt;id_index = sourceFieldsList.index('ID')&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;#Insert the field "ID" at the first index, remove old field&lt;/P&gt;
&lt;P&gt;sourceFieldsList.insert(0,sourceFieldsList.pop(id_index))&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;updateFieldsList = sourceFieldsList&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in updateRows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "-----------------------------"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keyValue = row[0]&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if keyValue in valueDict:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #print keyValue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for n in range(1,len(sourceFieldsList)):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Old value for ID %s %s:&amp;nbsp; %s" % (keyValue,updateFieldsList&lt;N&gt;,row&lt;N&gt;)&lt;/N&gt;&lt;/N&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Update with: %s" % (valueDict[keyValue][n-1])&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print ""&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row&lt;N&gt; = valueDict[keyValue][n-1]&lt;/N&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updateRows.updateRow(row)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;del valueDict&lt;/P&gt;
&lt;P&gt;del sourceFC&lt;/P&gt;
&lt;P&gt;del updateFC&lt;/P&gt;
&lt;P&gt;del id_index&lt;/P&gt;
&lt;P&gt;del row&lt;/P&gt;
&lt;P&gt;del updateRows&lt;/P&gt;
&lt;P&gt;del updateFieldsList&lt;/P&gt;
&lt;P&gt;del sourceFieldsList&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;However, the rows of updateFC are still not updating.&amp;nbsp; When I read the print generated inside the for loop starting line 27, the old values and new values are the same in for every row&lt;N&gt;.&lt;/N&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Aug 2014 20:02:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354067#M27828</guid>
      <dc:creator>BenColeman1</dc:creator>
      <dc:date>2014-08-28T20:02:22Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354068#M27829</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I see, I didn't realize the cursor could only be read through once if created outside of the loop.&amp;nbsp; I knew there was something having to do with the properties of the cursor that I didn't understand.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Aug 2014 20:05:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354068#M27829</guid>
      <dc:creator>BenColeman1</dc:creator>
      <dc:date>2014-08-28T20:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354069#M27830</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I understand that you are not seeing the updated values, but I am not clear whether the print statements are showing you the values you expect or not.&amp;nbsp;&amp;nbsp; I will assume that they are showing the correct values unless you tell me otherwise.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You have indented line 32 one level too far.&amp;nbsp; it should be outside of the loop that reads the fields, since it only needs to post the update of the row after all the fields of the row are updated.&amp;nbsp; Updating the row one field at a time will slow down the process needlessly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Do not del the updateRows variable.&amp;nbsp; The "with" cursor creation syntax automatically cleans up the cursor at the end.&amp;nbsp; I seem to recall that if you del that variable after the loop closes it messes things up with this syntax.&amp;nbsp; The variable Row becomes nothing by the time the cursor is fully read, so it does not need a del statement.&amp;nbsp; The other variables are simple strings, small lists and numbers and don't normally get del statements.&amp;nbsp; I only del the valueDict and never had any problems with memory leaks.&amp;nbsp; I would get rid of all the del statements except for the del valueDict.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Aug 2014 20:30:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354069#M27830</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-08-28T20:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354070#M27831</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;IN case you ever need to update SDE here is some code I am using to do that kind of editing.&amp;nbsp; I don't want to risk editing all 120k records at once, since SDE is touchy about bulk updates, so I have made the updateFC into a layer with a definition query to process fewer records.&amp;nbsp; The code below also illustrates that the field lists can be different between the source and the update feature class without major modifications to my code set up.&amp;nbsp; I am processing my records right now and it is updating my target FC extremely fast.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14092585164053652 jive_text_macro" jivemacro_uid="_14092585164053652"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;import os&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;sourceFC = r"L:\rfairhur\Layers\Local_Government\Local_Government.gdb\ReferenceData\RoadCenterline"&lt;/P&gt;
&lt;P&gt;updateFC = r"C:\Users\RFAIRHUR\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Trans Connection to SQL Server.sde\GDB_TRANS.TRANS.TRANSPORTATION_MAINT\CENTERLINE"&lt;/P&gt;
&lt;P&gt;arcpy.MakeFeatureLayer_management(updateFC, "CENTERLINE", "CL_ID &amp;gt; 10000 AND CL_ID &amp;lt; 20001")&lt;/P&gt;
&lt;P&gt;updateFC = "CENTERLINE"&lt;/P&gt;
&lt;P&gt;workspace = r"C:\Users\RFAIRHUR\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Trans Connection to SQL Server.sde"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;edit = arcpy.da.Editor(workspace)&amp;nbsp; &lt;/P&gt;
&lt;P&gt;edit.startEditing(False, True)&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;edit.startOperation()&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;sourceFieldsList = ["CL_ID", "ROUTE_ORIENTED", "ONEWAYDIR", "CALTRANS_FUNCTIONAL_CLASS", "FEDROUTE", "FEDRTETYPE", "STROUTE", "STRTETYPE", "CTYROUTE", "MUNILEFT", "MUNIRIGHT", "ZIPLEFT", "ZIPRIGHT"]&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;updateFieldsList = ["CL_ID", "ROUTE_ORIENTED", "TRAVEL_DIRECTIONS", "CA_FUNCTIONAL_CLASS", "FEDERAL_ROUTE", "FEDERAL_ROUTE_TYPE", "STATE_ROUTE", "STATE_ROUTE_TYPE", "COUNTY_ROUTE", "CITY_LEFT", "CITY_RIGHT", "ZIP_LEFT", "ZIP_RIGHT"]&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;print "Reading Records"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;print "Records are read"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for updateRow in updateRows:&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keyValue = updateRow[0]&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if keyValue in valueDict:&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for n in range (1,len(sourceFieldsList)):&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updateRow&lt;N&gt; = valueDict[keyValue][n-1]&amp;nbsp; &lt;/N&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updateRows.updateRow(updateRow)&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;del valueDict&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;edit.stopOperation()&amp;nbsp; &lt;/P&gt;
&lt;P&gt;edit.stopEditing(True)&lt;/P&gt;



&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Aug 2014 20:46:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354070#M27831</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-08-28T20:46:31Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354071#M27832</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I fixed the indention problem and that did the trick!&amp;nbsp; I will remove the superfluous dels.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Aug 2014 20:50:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354071#M27832</guid>
      <dc:creator>BenColeman1</dc:creator>
      <dc:date>2014-08-28T20:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354072#M27833</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am glad you got it all working.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That is interesting that the indent was the problem.&amp;nbsp; I suspect that only the first field was getting updated and after the row posted for the first field it would not accept any more field updates for that row.&amp;nbsp; Perhaps some type of record lock occurs in Python using the updateRow operation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anyway, this code will save lots of time over other data transfer techniques, like using joins and field calculations.&amp;nbsp; It is even possible to adapt the code to do on the fly multi-field concatenation of values to transfer data using a multi-field unique key when no single field can function as a One-to-One join field.&amp;nbsp; And the last loop that goes through the fields can be dropped and converted to use the actual field index numbers of the row and the valueDict when you need to do more complex field matching and individualized operations on the fields that go beyond a simple One-to-One data transfer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So I think you will find many uses for this code that can solve a lot of data relationship problems.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Aug 2014 21:09:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354072#M27833</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-08-28T21:09:37Z</dc:date>
    </item>
    <item>
      <title>Re: Nested for loops not iterating correctly with arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354073#M27834</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, I think this will prove to be very useful and flexible.&amp;nbsp; Thanks again for your help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Aug 2014 21:28:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nested-for-loops-not-iterating-correctly-with/m-p/354073#M27834</guid>
      <dc:creator>BenColeman1</dc:creator>
      <dc:date>2014-08-28T21:28:24Z</dc:date>
    </item>
  </channel>
</rss>

