AnsweredAssumed Answered

Create Query from List

Question asked by LANDVEST on Oct 27, 2016
Latest reply on Oct 27, 2016 by LANDVEST

There are several variations on this out there but all are use the OID/FID of the FC to create the query. I'd like to modify this to accommodate a different  field which is still an integer value.

The thing is arcpy.Describe makes it damn easy to get a list of FIDs. Am I missing something here or am I going to have to build a list by other means?

 

I do realize you could use a loop to create a super long query using 'OR' but given that the attribute values are integer,  I see no reason to use 'IN' for this.

 

Here's some code:

 

import arcpy

#Assumes the layer in ArcMap has a selected features
lyr = arcpy.mapping.Layer("yourLayer")

lyrDesc = arcpy.Describe(lyr)
fieldDesc = lyrDesc.FIDset
selectedList = fieldDesc.split(";")

#Build your query; the below is poor since it assumes "OBJECTID" is present; this is a draft
sql = "{} IN ({})".format("OBJECTID", ", ".join([str(n) for n in selectedList]))

#Apply the definition query
lyr.definitionQuery = sql

Outcomes