AnsweredAssumed Answered

How to iterate through a list to select by attribute?

Question asked by rhowell2 on Sep 16, 2019
Latest reply on Sep 17, 2019 by rhowell2

I am trying to iterate through a field (integer values 1, 2, 3), select by each unique attribute, and run a tool on that selection of points.

 

My list is:

NearValues = [1, 2, 3]

 

And my for loop is the following (again, I want to select all of the points with a value of 1, run the median center tool on all of those points, then select all of the 2's, etc):

for value in NearValues:
    query = "['nearstr']="+str(value)
    arcpy.SelectLayerByAttribute_management(points, "NEW_SELECTION", query)
    arcpy.MedianCenter_stats(points, "mediancenter_test" + str(value))

It's giving me an Invalid expression error, so I'm not sure if it's a minor syntax thing or if there's something wrong with my methodology. Here is the complete error if it's helpful:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 7605, in SelectLayerByAttribute
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 7602, in SelectLayerByAttribute
    retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

Outcomes