<?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: Whereclause in geoprocessing tool *select where item in list. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714131#M55386</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;this line makes no sense to me&lt;/P&gt;&lt;P&gt;values = [row[&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;]]&lt;/P&gt;&lt;P&gt;are you trying to append values to a values list?&amp;nbsp; if so values needs to be defined outside of the cursor and then row results appended to it&lt;/P&gt;&lt;P&gt;values = []&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;values.append(row[0]) # whatever&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then figure out what to do with the values list&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 11 Dec 2014 19:57:16 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2014-12-11T19:57:16Z</dc:date>
    <item>
      <title>Whereclause in geoprocessing tool *select where item in list.</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714130#M55385</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #262626; font-family: arial, sans-serif; font-size: 13px;"&gt;I am getting an incorrect SQL statement while trying to extractby attributes where item is in list.&amp;nbsp; Can somebody help me form the correct &lt;SPAN style="color: #262626; font-family: arial, sans-serif; font-size: 13px;"&gt;SQL &lt;/SPAN&gt;statement?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #262626; font-family: arial, sans-serif; font-size: 13px;"&gt;Line 30 below...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os
arcpy.env.overwriteOutput = 1


# Check out any necessary licenses
arcpy.CheckOutExtension("Spatial")


base = r'C:\Users\Noah\Desktop\LCP Example\Scratch.gdb'
name = "landuse_Clip"
fc= os.path.join(base,name)
field = "Value"


arcpy.env.workspace = base


# Use SearchCursor with list comprehension to return a
#&amp;nbsp; unique set of values in the specified field
#
for row in arcpy.da.SearchCursor(fc, "Value"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; values = [row[0]]


query = "Value" in values


# Extract Features where Value is in values
attExtract = ExtractByAttributes(name,&amp;nbsp; query)
attExtract.save("Extract_landuseRaster")
landuseRasterWeighted = "Extract_landuseRaster"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:34:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714130#M55385</guid>
      <dc:creator>NoahHuntington</dc:creator>
      <dc:date>2021-12-12T06:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Whereclause in geoprocessing tool *select where item in list.</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714131#M55386</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;this line makes no sense to me&lt;/P&gt;&lt;P&gt;values = [row[&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;]]&lt;/P&gt;&lt;P&gt;are you trying to append values to a values list?&amp;nbsp; if so values needs to be defined outside of the cursor and then row results appended to it&lt;/P&gt;&lt;P&gt;values = []&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;values.append(row[0]) # whatever&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then figure out what to do with the values list&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Dec 2014 19:57:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714131#M55386</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2014-12-11T19:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: Whereclause in geoprocessing tool *select where item in list.</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714132#M55387</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Dan. Thanks for chiming in.&amp;nbsp; I am just trying to return a list of unique values.&amp;nbsp; This revised portion of the script may have been less confusing. This one was also successful in printing the list in the desired format.&amp;nbsp; How can I use this now in my sql expression?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Use SearchCursor with list comprehension to return a
#&amp;nbsp; unique set of values in the specified field
#
values = [row[0] for row in arcpy.da.SearchCursor(fc, (field))]
uniqueValues = (values)
print(uniqueValues)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your help with this!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:34:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714132#M55387</guid>
      <dc:creator>NoahHuntington</dc:creator>
      <dc:date>2021-12-12T06:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Whereclause in geoprocessing tool *select where item in list.</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714133#M55388</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Beyond the issue identified by &lt;A href="https://community.esri.com/migrated-users/3116"&gt;Dan Patterson&lt;/A&gt;‌, you have two additional problems.&amp;nbsp; First, the SQL "IN" operator requires the set of values to be enclosed in parentheses, e.g., &lt;SPAN style="font-family: courier new,courier; font-size: 10pt;"&gt;expression IN (value1, value2, value3, ...)&lt;/SPAN&gt;.&amp;nbsp; Second, the ExtractByAttributes where_clause is a &lt;SPAN style="text-decoration: underline;"&gt;string-based&lt;/SPAN&gt; SQL expression, and you are passing a reference to a python object instead of an actual string-based list.&amp;nbsp; Assuming values is a Python list containing numbers you want to check for, line 30 would have to be rewritten as:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14183347089976706 jive_text_macro" jivemacro_uid="_14183347089976706"&gt;&lt;P&gt;query = "Value IN ({})".format(str(values).strip('[]'))&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Dec 2014 21:51:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714133#M55388</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2014-12-11T21:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Whereclause in geoprocessing tool *select where item in list.</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714134#M55389</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You don't get unique values that way, unless they are unique to start with.&amp;nbsp; In the following example, I got a gazillion values until I converted the list to a set (and optionally sorted).&amp;nbsp; From there, construct your query as &lt;A href="https://community.esri.com/migrated-users/3420" target="_blank"&gt;Joshua Bixby&lt;/A&gt;‌ indicated&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy
&amp;gt;&amp;gt;&amp;gt; fc = "C:\SomePath\SomeFile.shp"
&amp;gt;&amp;gt;&amp;gt; fld = "subclass"
&amp;gt;&amp;gt;&amp;gt; values = [row[0] for row in arcpy.da.SearchCursor(fc, (fld))]
&amp;gt;&amp;gt;&amp;gt; unique_values_set = set(values)
&amp;gt;&amp;gt;&amp;gt; unique_values_set
set([u'ARTERIAL', u'TRANSITWAY', u'LOCAL', u'RAMP', u'CONNECTOR', u'MAJCOLLECTOR', u'HIGHWAY', u'COLLECTOR', u'CHANNEL'])
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; unique_values_list = list(set(values))&amp;nbsp;&amp;nbsp;&amp;nbsp; # convert the set to a list and sort
&amp;gt;&amp;gt;&amp;gt; unique_values_list
[u'ARTERIAL', u'TRANSITWAY', u'LOCAL', u'RAMP', u'CONNECTOR', u'MAJCOLLECTOR', u'HIGHWAY', u'COLLECTOR', u'CHANNEL']&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just to be on the safe side... 'set' is a useful tool but it is unordered like dictionaries&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:34:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714134#M55389</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-12T06:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: Whereclause in geoprocessing tool *select where item in list.</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714135#M55390</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is code that combines Joshua and Dan's suggestions:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# get unique list of values
values = [row[0] for row in arcpy.da.SearchCursor(fc, (field))] 
uniqueValues = list(set(values)) # ["a","b","c"]
# create a SQL expression string in the form "FIELD IN ('a', 'b', 'c')"
where = "FIELD IN ({})".format(",".join(["'{}'".format(v) for v in uniqueValues])&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:34:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714135#M55390</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-12T06:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Whereclause in geoprocessing tool *select where item in list.</title>
      <link>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714136#M55391</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you all!!&amp;nbsp; You all seem very knowledgeable and have provided much useful info. Josh your query statement is precisely what I am looking for with this thread.&amp;nbsp; Below is the complete code for this operation I am posting for the archives. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# get unique list of values
values = [row[0] for row in arcpy.da.SearchCursor(fc, (field))]
query = "Value IN ({})".format(str(values).strip('[]'))

# Extract Features where Value is in values
attExtract = ExtractByAttributes(name,&amp;nbsp; query)
attExtract.save("Extract_landuseRaster")
landuseRasterWeighted = "Extract_landuseRaster"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:34:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/whereclause-in-geoprocessing-tool-select-where/m-p/714136#M55391</guid>
      <dc:creator>NoahHuntington</dc:creator>
      <dc:date>2021-12-12T06:34:49Z</dc:date>
    </item>
  </channel>
</rss>

