AnsweredAssumed Answered

arcpy.da.UpdateCursor use variable as field index?

Question asked by ehm119 on Feb 27, 2013
Latest reply on Feb 28, 2013 by ehm119
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.  I have a very simple operation I'd like to do -- calculate the newField = oldField for each pair.   I know I could use CalculateField_management, which is actually what I had been doing previously.  But there are conflicting reports as to which is faster -- da.UpdateCursor or CalculateField.  I am trying to optimize speed, so I want to try this out. 

The root of my question: is there a way to use a variable instead of the list index in a da cursor.  Specifically:

import arcpy FC = "C:\\data\\test.gdb\\test"  fields = ["newField1", "oldField1", "newField2", "oldField2", "newField3", "oldField3", .... "newFieldn", "oldFieldn"]  #There are actually a lot more fields in this same new/old pattern  with arcpy.da.UpdateCursor(FC, fields) as rows:     i = 0     for row in rows:         j = i + 1         row[i] = row[j]  #simply setting the new value = old value for each old/new pair, but get "list index out of range" error  i = i + 1

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.

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: 

baseFieldNameList = ["Field1", "Field2", "Field3", ...  "Fieldn") rows = arcpy.UpdateCursor for field in baseFieldNameList:     newField = "new" + field     oldField = "old" + field     oldValue = row.getValue(oldField)     row.setValue(newField, oldValue)     rows.updateRow del rows, row

Any suggestions, workarounds, or input would be much appreciated.