Expression for select by attribute

2680
4
Jump to solution
04-28-2015 04:49 AM
NaimeCelik
Occasional Contributor

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)

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

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)

View solution in original post

4 Replies
JakeSkinner
Esri Esteemed Contributor

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)
NaimeCelik
Occasional Contributor

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)

0 Kudos
NaimeCelik
Occasional Contributor

I also tried

ProcessedData = str(ProcessedData)[1:-1]

expression = "LineName IN (" + ProcessedData+ ")"

still same error

0 Kudos
NaimeCelik
Occasional Contributor

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

0 Kudos