Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

when running the following 2 sample codes, "[0]" is needed to be added after "row" in order to make code run well. I was confused. can anyone help explain? I know that it might be too easy for most of you from here.

sample code #1:

sf = r"C:\Data\Exercise07\alaska.shp"
cursor = arcpy.da.UpdateCursor(sf,["PERIMETER"])
for row in cursor:
...     if row[0] == 0.224:
...         print "find"

sample code #2:

cursor =arcpy.da.SearchCursor("alaska",["PERIMETER"],'"PERIMETER"=0.224')
for row in cursor:
...     print row[0]
share|improve this question
2  
This might not be the best explanation, but row[0] refers to the first item in a list (in this case a list of fields). Even though you only have one field PERIMETER, it's still a list. If you had more fields you could use row[1], row[2] etc. – Midavalo Mar 22 at 5:34
1  
This is commonly referred to as an access operator or sometimes [] operator. It is a very common idiom in programming languages/scripts to access objects/values stored in lists, vectors, maps, etc. Getting the vocab will help you greatly in the future when searching for answers. – RomaH Mar 22 at 15:52
    
Thank you so much, Midavalo & RomaH. – Carol Liu Mar 22 at 22:58
up vote 4 down vote accepted

In both your examples cursor is a Cursor object that consists of many Row objects.

You use the for loop to iterate through the Row objects which you have called row.

The Row object returned by arcpy.da.UpdateCursor() is a list of values. Understanding lists is a Python rather than ArcPy topic but think of it as comma-separated values enclosed in square brackets so if you print yours it will look like [123.456] (for one row) because it is a list of one floating point item.

To see this try running this code that I just tested at ArcGIS 10.4:

import arcpy

sf = r"C:\Data\Exercise07\alaska.shp"
cursor = arcpy.da.UpdateCursor(sf,["PERIMETER"])
for row in cursor:
    print row

If you loaded three fields (text, integer, text) into your cursor then your printed row might look something like ["A",23,"Road"].

Whenever you have a list (or a tuple, which is very similar) you use an index to get at its items.

If your row has only one item then you get at that using an index value of 0 i.e. row[0]. In the "list of 3" example to get at the second and third values I would use row[1] and row[2] respectively.

As identified by @AlexTereshenkov Search Cursors return tuples whereas Update Cursors return lists so note that my example uses Update Cursors but is quite similar to Search Cursors.

share|improve this answer
    
Shouldn't it be (123.456,) when you print a singleton tuple instead of (123.456)? The comma is what makes it a singleton tuple... – Alex Tereshenkov Mar 23 at 6:34
    
@AlexTereshenkov I thought you would be right but I just ran a test (now in my answer) that seems to show it looking more like a list than a tuple. – PolyGeo Mar 23 at 6:46
    
Got it now. For SearchCursor, you get a singleton tuple ('abc',). For UpdateCursor you get a list with a single item ['abc']. – Alex Tereshenkov Mar 23 at 6:51
    
@AlexTereshenkov Well spotted - that's a difference I had not expected but from their help pages: arcpy.da.SearchCursor() "Returns an iterator of tuples" while arcpy.da.UpdateCursor() "Returns an iterator of lists". – PolyGeo Mar 23 at 6:57
    
sometimes the arcgis python window produces unicode (like u'). Is there any reason? – Carol Liu Mar 23 at 18:01

row[0] simply refers to the first field in your list of fields in the cursor. Since you only have one field in your list, "PERIMETER", then row[0] refers to that field. If you had multiple fields, ["PERIMETER","AREA","POPULATION"] that were being searched or updated in your cursors, then

row[0] would be "PERIMETER"
row[1] would be "AREA"
row[2] would be "POPULATION"

It is simply how python access elements in a list.

share|improve this answer

Great answers. But it looks as if the row itself was not shown in the answers.

Another thing that is good to know is that when a single field is supplied, a singleton tuple is returned (row in this case):

with arcpy.da.SearchCursor(fc,"PROPERTY_ID") as search_cur:
    for row in search_cur:
        print row
        break

 >>> (5001,)

It's easy to understand that this is a singleton tuple by looking at the trailing comma it has. By the way, you don't need to supply a list of fields when you have just one field. This line would also work for you:

cursor = arcpy.da.SearchCursor("alaska","PERIMETER",'"PERIMETER"=0.224')

Please use the with statement when constructing the cursors; this will handle cleaning up and deleting the objects when they've been used and are no longer needed. Take a look at the Esri samples here and other samples here.

A handy way to access all of the features within a feature class without using the for iteration, is a list comprehension:

feats = [feat for feat in arcpy.da.SearchCursor(fc,"PROPERTY_ID")]
#printing first three elements
print feats[:3]
>>> [(5001,), (5002,), (1003,)]

The feats can be referred to as a list of singleton tuples. Fairly often, you would like to get a list of those values without having tuples at all. Again, list comprehension comes in very handy.

feats = [feat[0] for feat in arcpy.da.SearchCursor(fc,"PROPERTY_ID")]
#printing first three elements
print feats[:3]
>>> [5001, 5002, 1003]

The feat[0] returns a first item in the singleton tuple.

share|improve this answer
    
very useful resources, thank you! I went through a couple of python tutorials before reading "python scripting for arcgis - by Zandbergen" (which I am doing now), but it seemed my knowledge on python was not solid enough. long way to go :-( – Carol Liu Mar 22 at 22:57
1  
Glad it's helpful. Keep learning Python, it's one of the best investments you can make in your GIS career. Zandbergen is imho the best book to learn Python within ArcGIS, and I've read nearly all of them; you are in good hands :) Check a post on arcpy resources to learn here: gis.stackexchange.com/questions/53816/… and for more Python here gis.stackexchange.com/questions/3001/… – Alex Tereshenkov Mar 23 at 6:32
    
thanks. I will definitely read those! – Carol Liu Mar 23 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.