<?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: Pass dictionary key to arcpy.SelectLayerByAttribute_management in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067397#M61338</link>
    <description>&lt;P&gt;My bad I thought I posted the whole code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;fc = "Wells"
fc2 = "Polygon"

fcList = []
fc2List = []

with arcpy.da.SearchCursor(fc, ["PIN"]) as cursor:
    for row in cursor:
        fcList.append(row[0])
        #print row[0]

with arcpy.da.SearchCursor(fc2, ["PIN"]) as cursor:
    for row in cursor:
        if row[0] in fcList:
            fc2List.append(row[0])
            print row[0]


#arcpy.MakeFeatureLayer_management(fc2, "fc2_Lyr")

for WellID in fc2List:
    arcpy.SelectLayerByAttribute_management("fc2, "NEW_SELECTION", "\"PIN\" = '{}'".format(WellID))
#arcpy.MakeFeatureLayer_management(fc2, "SelPar_lyr", "\"PIN\" = '{}'".format(WellID))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Jun 2021 16:20:25 GMT</pubDate>
    <dc:creator>TonyAlmeida</dc:creator>
    <dc:date>2021-06-11T16:20:25Z</dc:date>
    <item>
      <title>Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067379#M61336</link>
      <description>&lt;P&gt;&amp;nbsp;I am trying to compare the PIN fields of two layers, fc1 and fc2 and select fc2 features. The issues I have is that this process one PIN at a time it doesn't just select them all at once.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;#arcpy.MakeFeatureLayer_management(fc2, "fc2_Lyr")

for WellID in fc2List:
    arcpy.SelectLayerByAttribute_management(fc2, "NEW_SELECTION", "\"PIN\" = '{}'".format(WellID))

#arcpy.MakeFeatureLayer_management(fc2, "SelPar_lyr", "\"PIN\" = '{}'".format(WellID))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 16:19:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067379#M61336</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2021-06-11T16:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067391#M61337</link>
      <description>&lt;P&gt;Not entirely sure what you mean, is it to select everything in fc2list?&lt;/P&gt;&lt;P&gt;If so, maybe just get rid of the for loop and use the SQL 'IN' statement.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.SelectLayerByAttribute_management("fc2_Lyr", "NEW_SELECTION", "\"PIN\" IN ({})".format(fc2List))&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 11 Jun 2021 16:01:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067391#M61337</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-06-11T16:01:01Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067397#M61338</link>
      <description>&lt;P&gt;My bad I thought I posted the whole code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;fc = "Wells"
fc2 = "Polygon"

fcList = []
fc2List = []

with arcpy.da.SearchCursor(fc, ["PIN"]) as cursor:
    for row in cursor:
        fcList.append(row[0])
        #print row[0]

with arcpy.da.SearchCursor(fc2, ["PIN"]) as cursor:
    for row in cursor:
        if row[0] in fcList:
            fc2List.append(row[0])
            print row[0]


#arcpy.MakeFeatureLayer_management(fc2, "fc2_Lyr")

for WellID in fc2List:
    arcpy.SelectLayerByAttribute_management("fc2, "NEW_SELECTION", "\"PIN\" = '{}'".format(WellID))
#arcpy.MakeFeatureLayer_management(fc2, "SelPar_lyr", "\"PIN\" = '{}'".format(WellID))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 16:20:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067397#M61338</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2021-06-11T16:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067422#M61340</link>
      <description>&lt;P&gt;A variation on&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;'s theme.&amp;nbsp; Here I use list comprehension to create a list of the Wells pin values and then select the Polygons if the PIN is in the list.&amp;nbsp; You mentioned using a dictionary in the subject and you can use dictionary comphrension the same way. FYI: untested.&amp;nbsp; you may need to fiddle with the select statement...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy

fc = "Wells"
fc2 = "Polygon"
fields = ['PIN']

fcList = [i[0] for i in arcpy.da.SearchCursor(fc,fields)]


arcpy.MakeFeatureLayer_management(fc2, "fc2_Lyr")

select = f'PIN IN {list}'
arcpy.SelectLayerByAttribute_management("fc2_Lyr", 'ADD_TO_SELECTION', select)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 17:09:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067422#M61340</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-06-11T17:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067447#M61341</link>
      <description>&lt;P&gt;I like this, but don't forget that formatting your &lt;FONT face="courier new,courier"&gt;select&lt;/FONT&gt; string with the list isn't valid SQL syntax because of the brackets. You need to reformat it with parenthesis. Assuming PIN is a string, you would need&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;select = f'PIN IN({",".join(fcList)})'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 17:36:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067447#M61341</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-11T17:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067449#M61342</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/191789"&gt;@BlakeTerhune&lt;/a&gt;&amp;nbsp;That's exactly why I included the disclaimer!&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 17:39:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067449#M61342</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-06-11T17:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067489#M61343</link>
      <description>&lt;P&gt;ok, thanks for the suggestion. It is not liking the&amp;nbsp; expression. This is currently in a shapefile.&lt;BR /&gt;Failed to execute (SelectLayerByAttribute). PIN is a string&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;arcpy.MakeFeatureLayer_management(fc2, "fc2_Lyr")
result = arcpy.GetCount_management("fc2_Lyr").getOutput(0)
print ('{} has {} records'.format("fc2_Lyr", result[0]))
select = f'PIN IN({",".join(fc2List)})'
arcpy.SelectLayerByAttribute_management("fc2_Lyr", 'ADD_TO_SELECTION', select)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 19:09:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067489#M61343</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2021-06-11T19:09:23Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067510#M61344</link>
      <description>&lt;LI-CODE lang="python"&gt;pinList = ['123','456','789']

'123' in pinList
Out[2]: True

'123' in ','.join(pinList)
Out[4]: True&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Like I said, you'll need to fiddle a bit with the selection statement.&amp;nbsp; It typically takes me a time or two to get them right,&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 19:29:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067510#M61344</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-06-11T19:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067548#M61345</link>
      <description>&lt;P&gt;Ah, I forgot the single quotes. As&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3374"&gt;@JoeBorgione&lt;/a&gt;&amp;nbsp;mentioned, these often take some fiddling. Hopefully this will keep the strings as single quotes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;select = f"PIN IN({str(fc2List)[1:-1]})"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&lt;/P&gt;&lt;P&gt;I just found &lt;A href="https://stackoverflow.com/a/16054895" target="_self"&gt;this solution&lt;/A&gt; that is more robust and less of a hack.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;str_values = ",".join(f"'{x}'" for x in fc2List)
select = f"PIN IN({str_values})"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 20:41:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067548#M61345</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-11T20:41:58Z</dc:date>
    </item>
    <item>
      <title>Re: Pass dictionary key to arcpy.SelectLayerByAttribute_management</title>
      <link>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067635#M61350</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/appendices/geoprocessing-considerations-for-shapefile-output.htm" target="_blank"&gt;Geoprocessing considerations for shapefile output—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;dBASE files have little SQL support aside from a WHERE clause.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jun 2021 03:04:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pass-dictionary-key-to-arcpy/m-p/1067635#M61350</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-06-12T03:04:41Z</dc:date>
    </item>
  </channel>
</rss>

