# Getting XY coordinates and field values for each row in ArcPy SearchCursor?

I am trying to enumerate each row of a points shapefile and get a certain field's value (elevation) and the xy coordinates.

But I see I can only use:

• arcpy.SearchCursor to get the field; OR
• arcpy.da.SearchCursor(fc, "SHAPE@XY") for the xy coordinates

Can I do both at once?

Yes - the code below uses the SHAPE@XY token and a field called elevation to do it on a test feature class:

``````import arcpy

fc = r"C:\temp\test.gdb\pointFC"
fields = ['elevation', 'SHAPE@XY']

with arcpy.da.SearchCursor(fc, fields) as cursor:

for row in cursor:
elev = row
coordX = row
coordY = row
print "X: {}, Y: {}, Elevation: {}".format(coordX,coordY,elev)
``````
• PolyGeo's is correct. It should not be assumed the feature class (and the OP's language confirms this) has explicit x and y fields in it. In addition to PolyGeos answer, you can use fields = ['SHAPE@Y', 'SHAPE@X'] Aug 10 '16 at 18:22

Yes, you just have to define additional fields to search against, see e.g.

``````fields = ['elevation', 'x', 'y']

with arcpy.da.SearchCursor(fc, fields) as cursor:

for row in cursor:
elev = row
coordX = row
coordY = row
``````
• This assumes that there are fields called 'x' and 'y' that contain the XY coordinate values but the Question indicates that these are to be pulled from the SHAPE@XY token.
– PolyGeo
Nov 15 '13 at 3:05
• @PolyGeo, Yes I may be assuming that x and y are existing fields, but to keep it simple I just separated those fields out in my list. Nov 15 '13 at 3:21
• I like PolyGeo's answer, but I like yours a bit more as it's a bit more obvious especially for beginners. Nov 18 '13 at 6:45