<?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 Create selection from Python list in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/create-selection-from-python-list/m-p/466414#M15656</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I can't tell exactly what you are trying to do, but here's an example of using items in a list for selections using a query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
myList = ["apple","pear","orange"]
for item in myList:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select item in list based on field COUNTRY.
&amp;nbsp;&amp;nbsp;&amp;nbsp; qry = "\"COUNTRY\" = '"+item+"'"

&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Select_analysis(inFile, outDir, qry)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 20:41:16 GMT</pubDate>
    <dc:creator>RDHarles</dc:creator>
    <dc:date>2021-12-11T20:41:16Z</dc:date>
    <item>
      <title>Create selection from Python list</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/create-selection-from-python-list/m-p/466413#M15655</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Dear all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was wondering if it is possible to create a query for a selection from a Python list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The Python list is created from features in a FeatureClass but since the geoprocessor is very slow I write all the features into a Python list and do all the analysis with Python lists for better performance.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;While the performance of getting all the features of interest is good, I do not know how to select all the features in the actual FeatureClass based on my resulting list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If someone has an example for?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance, Thomas&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Apr 2010 07:30:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/create-selection-from-python-list/m-p/466413#M15655</guid>
      <dc:creator>TomGeo</dc:creator>
      <dc:date>2010-04-16T07:30:48Z</dc:date>
    </item>
    <item>
      <title>Create selection from Python list</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/create-selection-from-python-list/m-p/466414#M15656</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I can't tell exactly what you are trying to do, but here's an example of using items in a list for selections using a query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
myList = ["apple","pear","orange"]
for item in myList:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select item in list based on field COUNTRY.
&amp;nbsp;&amp;nbsp;&amp;nbsp; qry = "\"COUNTRY\" = '"+item+"'"

&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Select_analysis(inFile, outDir, qry)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:41:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/create-selection-from-python-list/m-p/466414#M15656</guid>
      <dc:creator>RDHarles</dc:creator>
      <dc:date>2021-12-11T20:41:16Z</dc:date>
    </item>
    <item>
      <title>Creating queries with keys</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/create-selection-from-python-list/m-p/466415#M15657</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, this is a very successful way of creating an SQL query that is efficient.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A keyfile type selection is often wanted, and the MakeQueryTable is buggy at 9.3 and has a lot of limitations.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You have to have all features in the same geodatabase, complex expressions are not supported in file geodatabase and so on.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is my workaround:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Loop through the first table with a cursor to collect a list of keys.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Pass it through a SET structure to remove duplicates.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Create a (potentially huge) sql expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Use this expression on the source featureclass to create a layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Process or export the layer to a new featureclass&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# to make a keyfile
# 26 April 2010 kimo
# much better than MakeQueryTable and it works across geodatabases
# still uses an SQL query but python generates the set for the expression
# will work for tens of thousands of keys (amazing)
# if you want to save the featureclass or table as a layer or view definition
# to use in ArcMap (without making a copy) you MUST have an index on the key field
# or it will be extremely slow to draw.
# see the Keyfile tool in ArcScripts for an alternative method using selection sets in ArcMap

import arcgisscripting,sys,os,datetime,fileinput
gp = arcgisscripting.create(9.3)

def titlekey(title) :
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Workspace = ws
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a title set to use as a keyfile
&amp;nbsp;&amp;nbsp;&amp;nbsp; lstTitle = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; cur = gp.SearchCursor(title)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = cur.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp; while row :
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lstTitle.append(row.ttl_title_no)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = cur.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp; del row,cur
&amp;nbsp;&amp;nbsp;&amp;nbsp; return set(lstTitle)

def title_tab(src) :
&amp;nbsp;&amp;nbsp;&amp;nbsp; " use setTitle to export records to a table"
&amp;nbsp;&amp;nbsp;&amp;nbsp; start = datetime.datetime.now()
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Workspace = ws
&amp;nbsp;&amp;nbsp;&amp;nbsp; setTitle = titlekey(title)
&amp;nbsp;&amp;nbsp;&amp;nbsp; expTitle = ",".join(["'"+t+"'" for t in setTitle])
&amp;nbsp;&amp;nbsp;&amp;nbsp; sqlQuery = "ttl_title_no in ("+expTitle+")"
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeTableView_management(src,src+"_view",sqlQuery,wsout)
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.OverwriteOutput = True
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CopyRows_management(src+"_view",wsout+"/"+src+"_sub")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print gp.GetCount(wsout+"/"+src+"_sub").Getoutput(0),"records in "+src+"_sub"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print datetime.datetime.now() - start
# ------------------------- main -------------------------
title = "e:/arrears.mdb/title"
ws = "e:/memo.gdb"
wsout = "e:/arrears.mdb"
# create subsets based on title keys
if not gp.Exists(wsout+"/tta_sub") :
&amp;nbsp;&amp;nbsp;&amp;nbsp; title_tab("tta")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:41:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/create-selection-from-python-list/m-p/466415#M15657</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2021-12-11T20:41:18Z</dc:date>
    </item>
  </channel>
</rss>

