Hello, I have unique list like ProcessedData=["Line1","Line17","Line76"....] and I have a point data in which every point has the line name they belong to in its attribute table. I want to select those points based on their line names but only the ones in that unique list. I am applying the python code below but processing is very slow. I need to select 10000 lines and the process is very slow with the code below. Is there any other way of writing the expression so that it selects them in same time instead of one by one in a for loop ?
for item in ProcessedData:
Expression= "\"LineName\" = "+ "'"+item+"'"
arcpy.SelectLayerByAttribute_management ("PointData", "ADD_TO_SELECTION", Expression)
Solved! Go to Solution.
Hi Naime,
Instead of iterating through your list with a 'for' loop, try using 'IN' within your expression to select multiple values. Ex:
list = ['Alabama', 'Arizona'] list = str(list)[1:-1] expression = "State_Name IN (" + list + ")" arcpy.SelectLayerByAttribute_management("COUNTIES", "NEW_SELECTION", expression)
Hi Naime,
Instead of iterating through your list with a 'for' loop, try using 'IN' within your expression to select multiple values. Ex:
list = ['Alabama', 'Arizona'] list = str(list)[1:-1] expression = "State_Name IN (" + list + ")" arcpy.SelectLayerByAttribute_management("COUNTIES", "NEW_SELECTION", expression)
Hello Jake,
Thank you for your reply.
When I inserted my expression as you indicated
ProcessedData = str(ProcessedData)[1:-1]
expression = "LineName (" + ProcessedData+ ")"
I got error as below, why do you think this happens?
ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute)
I also tried
ProcessedData = str(ProcessedData)[1:-1]
expression = "LineName IN (" + ProcessedData+ ")"
still same error
Hello Jake
I just realized that the problem was encoding in the list items were look like u'Line1' after turning each item
ProcessedData=[str(x) for x in ProcessedData]
then it worked very well Thank you so much for your answer