<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Selection to definition query Python script — Works for layers but not standalone tables in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1372121#M69690</link>
    <description>&lt;P&gt;So, the problem appears to be that you’re looking for a table in listlayers(). Tables aren’t considered layers since they don’t draw.&lt;/P&gt;&lt;P&gt;Try listTables() instead and see what else you get.&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/3.1/arcpy/mapping/map-class.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/3.1/arcpy/mapping/map-class.htm&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jan 2024 06:51:15 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2024-01-19T06:51:15Z</dc:date>
    <item>
      <title>Selection to definition query — Python script works for layers but not standalone tables</title>
      <link>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1372120#M69689</link>
      <description>&lt;P&gt;ArcGIS Pro 3.2.1&lt;/P&gt;&lt;P&gt;I have a Python script that I use in the Python window to convert a selection to a definition query.&lt;/P&gt;&lt;PRE&gt;def qdef_selected_features(lyr):
    desc = arcpy.Describe(lyr)
    # Get a semicolon-delimited string of selected feature IDs 
    fid_list = desc.FIDSet.split(";")
    # build the query definition
    query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
    # apply the query definition back to the layer   
    lyr.definitionQuery = query

aprx = arcpy.mp.ArcGISProject('current') 
m = aprx.activeMap 
lyr = m.listLayers('&lt;STRONG&gt;MY_USER.ROADS&lt;/STRONG&gt;')[0] 
qdef_selected_features(lyr)&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;PRE&gt;OBJECTID IN (6, 7, 17, 18, 19, 23, 27, 28)&lt;/PRE&gt;&lt;P&gt;The script works for feature layers but doesn't work for standalone tables.&lt;/P&gt;&lt;PRE&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 12, in &amp;lt;module&amp;gt;&lt;BR /&gt;IndexError: list index out of range&lt;/PRE&gt;&lt;P&gt;How can I enhance the script so that it works for non-spatial tables, too?&lt;/P&gt;&lt;P&gt;I'm a novice. Any other tips about improvements would be welcome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Related:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/selection-to-definition-query/idc-p/1272879/highlight/true#M24101" target="_self"&gt;Selection to Definition Query&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://gis.stackexchange.com/questions/368848/creating-definition-query-from-selected-features-using-arcgis-pro/456318#456318" target="_self" rel="nofollow noopener noreferrer"&gt;Creating definition query from selected features using ArcGIS Pro&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Fri, 19 Jan 2024 06:53:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1372120#M69689</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-19T06:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Selection to definition query Python script — Works for layers but not standalone tables</title>
      <link>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1372121#M69690</link>
      <description>&lt;P&gt;So, the problem appears to be that you’re looking for a table in listlayers(). Tables aren’t considered layers since they don’t draw.&lt;/P&gt;&lt;P&gt;Try listTables() instead and see what else you get.&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/3.1/arcpy/mapping/map-class.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/3.1/arcpy/mapping/map-class.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 06:51:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1372121#M69690</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2024-01-19T06:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: Selection to definition query — Python script works for layers but not standalone tables</title>
      <link>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1372142#M69691</link>
      <description>&lt;P&gt;and in the Tables class&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/table-class.htm" target="_blank"&gt;Table—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;There is&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;listDefinitionQueries ({wildcard})&lt;BR /&gt;Returns a Python list of definition queries associated with a table.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;and the ability to get or set them&lt;/P&gt;&lt;P&gt;definitionQuery&lt;BR /&gt;(Read and Write)&lt;BR /&gt;Provides the ability to get or set a tables's definition query.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 09:01:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1372142#M69691</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2024-01-19T09:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Selection to definition query — Python script works for layers but not standalone tables</title>
      <link>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1561234#M73218</link>
      <description>&lt;P&gt;Functionality added in ArcGIS Pro 3.4:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/generate-definition-query-from-selection.htm" rel="nofollow noreferrer" target="_blank"&gt;Generate Definition Query From Selection&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/selection-to-definition-query/idi-p/932030/highlight/true" rel="nofollow noreferrer" target="_blank"&gt;Selection to Definition Query&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Thu, 21 Nov 2024 14:23:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-to-definition-query-python-script-works/m-p/1561234#M73218</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-11-21T14:23:42Z</dc:date>
    </item>
  </channel>
</rss>

