2

This question already has an answer here:

I am trying to get arcpy to select features in a shapefile that appear in a list generated from a model output file in a variable called 'died'.

The list looks like this:

died
['acadian022', 'acadian051', 'acadian052', 'acadian121', 'acadian152', 
'acadian191', 'chick031', 'chick032', 'chick052', 'chick132', 'creeper032', 
'downy032', 'downy042', 'kentucky041', 'ovenbird012', 'ovenbird061', 
'pewee011', 'pewee021', 'pewee032', 'pewee051', 'pewee071', 'pewee082', 
'pewee101', 'thrush022', 'thrush042', 'thrush152']

I can get arcpy to select one of these points using this:

inFeatures = "nests"
outFeatures = "C:/test.gdb/new_nests"
tempLayer = "nestslyr"
expression = "ID = 'acadian022'"

arcpy.CopyFeatures_management(inFeatures, outFeatures)
arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)  
arcpy.SelectLayerByAttribute_management(tempLayer,"ADD_TO_SELECTION",
expression)

But when I change this to a for loop, the loop runs fine and I do not get any errors, but the tool does not select any points.

for x in died:
    expression = "ID = 'x'"
    arcpy.SelectLayerByAttribute_management(tempLayer,"ADD_TO_SELECTION",
    expression)

Does anyone know why this might be the case?

marked as duplicate by Hornbydd, whyzar, tinlyx, Evil Genius, HDunn Oct 5 '17 at 17:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    It's not working because your expression is not using the variable x, it is using the string x. See this answer about building expressions using variables: gis.stackexchange.com/questions/27457/… – Jacob F Oct 5 '17 at 14:57
  • that's what I needed. I was googling and searching around here for hours and never found this, so thank you! – Laura Oct 5 '17 at 15:00

Browse other questions tagged or ask your own question.