3

I am looking for guidance regrading the usage or meaning of the square brackets in arcpy update cursor (Win 7, ArcGIS 10.2, Python 2.7.5) Although I have serched long and hard with various web searches, incl https://docs.python.org/2/library/index.html, I have not been able to find a descriptive answer.

So, in the example below (from post Help with Python Update Cursor syntax) I am trying to figure what the numbers within the [] actually mean?

Is it the column number?

If so, does the count start from 0 and from the left?

if(row[4] == crow.STTYPE and row[15]=="B":
 crow.FIELD1 = "HI"
 crows.updateRow(crow)
else:
 crow.FIELD1 = "BYE"
 crows.updateRow(crow)
asked Aug 19, 2015 at 22:38
3
  • 1
    They are your field indexes. If you haven't subsetted your fields when creating the cursor they are the feature class field indexes in order. Each row emulates a list object which can be indexed via square brackets, when creating a row using the insert cursor a row can be inserted into a table using a list. Note: this only applies to arcpy.da. cursors and not arcpy. cursors... does that help? Commented Aug 19, 2015 at 22:46
  • Thnaks @MichaelMiles-Stimson , I think so but just to confirm, [4] would be field 4 from my fc and [15] would be field 15.? (unless I subset them like [this, that, the_other, red, blue, green] in which case [4] would = 'red'?) Cheers, Peter Commented Aug 19, 2015 at 22:57
  • 1
    The indices are 0 based, usually [0] is OBJECTID/FID. So if your table has the fields FID, SHAPE, Red, Green, Blue then row[2] is the 'Red' field. Should you want to only access Red, Green and Blue fields declare your cursor like arcpy.da.UpdateCursor(YourData,fields=['Red','Green','Blue']) then row[0] is red. Commented Aug 20, 2015 at 0:04

1 Answer 1

4

Square brackets aren't used for the old version of cursors. Note that in the link your provided, the brackets are indexing a python list (alpha), not a cursor. For the newer data access version of cursors (da.SearchCursor, etc.), the brackets are used to reference the field index. For example, say you have a feature class and you want to iterate through its rows and find the information stored in three fields, such as:

inFc = r"c:\test\test.gdb\test" #feature class
fld1 = "field1" #field 1 name 
fld2 = "field2"
fld3 = "field3"

The cursor would be generated in the data access cursor as such:

cursor = arcpy.da.SearchCursor (inFc, [fld1, fld2, fld3])

You can then iterate through your rows and print the row's values in the three fields as such:

for row in cursor:
 print row[0] #prints first field's value
 print row[1] #prints second field's value
 print row[2] #prints third field's value

The same thing is accomplished in the older version of the cursor with:

cursor = arcpy.SearchCursor (fc)
for row in cursor:
 print row.getValue (fld1) #print first field's value
 ...

The overarching concept is that square brackets in python reference indexes in an object that can be iterated. The indexing starts at 0 for the first item.

>>> ["a","b","c","d"] [2]
'c'
>>> "abcd"[3]
'd'

I hope this helps.

answered Aug 19, 2015 at 23:03
1
  • Perfect explanation thanks @EmilBrundage particulalry as I have existing scripts, some using SearchCursor and somme using da.SearchCursor. Commented Aug 19, 2015 at 23:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.