<?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: arcpy.da.UpdateCursor use variable as field index? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-da-updatecursor-use-variable-as-field-index/m-p/491136#M38425</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You are getting an index error because you never reset your index back to 0 for the next row. This example should get you a little further.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;with arcpy.da.UpdateCursor(FC, fields) as rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 0 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while i &amp;lt; len(fields):&amp;nbsp; #for field in fields: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row&lt;I&gt; = row[i + 1] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 2 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 27 Feb 2013 19:34:03 GMT</pubDate>
    <dc:creator>MathewCoyle</dc:creator>
    <dc:date>2013-02-27T19:34:03Z</dc:date>
    <item>
      <title>arcpy.da.UpdateCursor use variable as field index?</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-updatecursor-use-variable-as-field-index/m-p/491135#M38424</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm working with an arcpy data access update cursor with a lot of fields that have an old/new pattern (e.g. oldField1, newField1, oldField2, newField2, etc.&amp;nbsp; I have a very simple operation I'd like to do -- calculate the newField = oldField for each pair.&amp;nbsp;&amp;nbsp; I know I could use CalculateField_management, which is actually what I had been doing previously.&amp;nbsp; But there are &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.arcgis.com/threads/65388-I-need-help-on-the-Update-Cursor-function?p=227108&amp;amp;viewfull=1#post227108" rel="nofollow" target="_blank"&gt;conflicting reports as to which is faster&lt;/A&gt;&lt;SPAN&gt; -- da.UpdateCursor or CalculateField.&amp;nbsp; I am trying to optimize speed, so I want to try this out.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The root of my question: is there a way to use a variable instead of the list index in a da cursor.&amp;nbsp; Specifically:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy FC = "C:\\data\\test.gdb\\test"&amp;nbsp; fields = ["newField1", "oldField1", "newField2", "oldField2", "newField3", "oldField3", .... "newFieldn", "oldFieldn"]&amp;nbsp; #There are actually a lot more fields in this same new/old pattern&amp;nbsp; with arcpy.da.UpdateCursor(FC, fields) as rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; i = 0 &amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; j = i + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row&lt;I&gt; = row&lt;J&gt;&amp;nbsp; #simply setting the new value = old value for each old/new pair, but get "list index out of range" error&amp;nbsp; i = i + 1&lt;/J&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know I could take the actual index for each (eg row[0] = row[1], then row[2] = row[3] the next time, etc) but that would be messy with so many fields. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With the old cursors, I could have done something like the following, but the old cursors are definitely slower than CalculateField_management, so this is not worth pursuing:&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;baseFieldNameList = ["Field1", "Field2", "Field3", ...&amp;nbsp; "Fieldn") rows = arcpy.UpdateCursor for field in baseFieldNameList: &amp;nbsp;&amp;nbsp;&amp;nbsp; newField = "new" + field &amp;nbsp;&amp;nbsp;&amp;nbsp; oldField = "old" + field &amp;nbsp;&amp;nbsp;&amp;nbsp; oldValue = row.getValue(oldField) &amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(newField, oldValue) &amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow del rows, row&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions, workarounds, or input would be much appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-Erik&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Feb 2013 18:52:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-updatecursor-use-variable-as-field-index/m-p/491135#M38424</guid>
      <dc:creator>ErikMartin</dc:creator>
      <dc:date>2013-02-27T18:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.UpdateCursor use variable as field index?</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-updatecursor-use-variable-as-field-index/m-p/491136#M38425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You are getting an index error because you never reset your index back to 0 for the next row. This example should get you a little further.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;with arcpy.da.UpdateCursor(FC, fields) as rows: &amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = 0 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while i &amp;lt; len(fields):&amp;nbsp; #for field in fields: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row&lt;I&gt; = row[i + 1] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 2 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Feb 2013 19:34:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-updatecursor-use-variable-as-field-index/m-p/491136#M38425</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-02-27T19:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.da.UpdateCursor use variable as field index?</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-da-updatecursor-use-variable-as-field-index/m-p/491137#M38426</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Matthew... your edited solution worked.&amp;nbsp; The two pieces I needed to change were resetting the index back to 0 for each row and using the while loop instead of the for loop.&amp;nbsp; As I'm interpreting this, the problem with the for loop would be that it was still trying to advance by two fields even for the last field, when of course there are no fields after the last field (using the for loop even with the index set to 0 for each row still results in a "list index out of range" error.).&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, for the record, the data access cursor was far faster than the field calculator in my case with 51 pairs of fields &amp;amp; 4000 records.&amp;nbsp; The data access cursor took 1 second.&amp;nbsp; The field calculator took 73 seconds... a much bigger difference than I expected and really significant for a web application (this is feeding an asynchronous GP service).&amp;nbsp; Thanks for the help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-Erik&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Feb 2013 11:44:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-da-updatecursor-use-variable-as-field-index/m-p/491137#M38426</guid>
      <dc:creator>ErikMartin</dc:creator>
      <dc:date>2013-02-28T11:44:40Z</dc:date>
    </item>
  </channel>
</rss>

