<?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 within Nested Loop Not Working as Expected in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/selection-within-nested-loop-not-working-as/m-p/1153980#M64060</link>
    <description>&lt;P&gt;Maybe I'm missing something but you've applied a selection to new_layer then not done anything with it after.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#arcpy.Identity_analysis(trawlBuffs, fishnet, out_feat)
#do you mean?
arcpy.Identity_analysis(new_layer, fishnet, out_feat)&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 15 Mar 2022 19:16:58 GMT</pubDate>
    <dc:creator>DavidPike</dc:creator>
    <dc:date>2022-03-15T19:16:58Z</dc:date>
    <item>
      <title>Selection within Nested Loop Not Working as Expected</title>
      <link>https://community.esri.com/t5/python-questions/selection-within-nested-loop-not-working-as/m-p/1153962#M64059</link>
      <description>&lt;P&gt;Thought this was fixed yesterday, mistaken.&amp;nbsp; I have two sets of polygons.&amp;nbsp; One featureclass has different fields for YEAR (integer), and GEAR type (string).&amp;nbsp; I am trying to loop through each of these two fields and do an Identity with the other polygon.&amp;nbsp; Output I am looking for will ONLY contain the polygons for each year and gear combination.&amp;nbsp; The code seems to run without error, but I get the exact same number of records for each identity output, so the selection within the loop clearly is not working right.&amp;nbsp; I am using a function to get the unique values for the year and gear fields.&amp;nbsp; Wondering what I am messing up?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
#Set the data environment 
arcpy.env.overwriteOutput = True

arcpy.env.workspace = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb'
trawlBuffs = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb\buffers\trawl_buffers'
fishnet = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb\fishnets\vms_net1k'
spidentityOut = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb\sp_identity\fishnet_'

# function to list unique values of a field within a fc
def unique_values(table, field):
    with arcpy.da.SearchCursor(table, [field]) as cursor:
        return sorted({row[0] for row in cursor})
    del cursor
# Get the list of values for the YEAR' field within the 'trawl_buffs' fc and place into a set
yearVals = unique_values(trawlBuffs, "YEAR")
unique_year = set(yearVals)
# Get the list of values for the 'AGENCY_GEAR' field within the 'trawl_buffs' fc and place into a set
gearVals = unique_values(trawlBuffs, "AGENCY_GEAR")
unique_gear = set(gearVals)
#loop through the years/gears with a selectionClause, and do the identity
try:
    for year in unique_year:
        for gear in unique_gear:
            new_layer = str(year) + str(gear) 
            arcpy.MakeFeatureLayer_management(trawlBuffs, new_layer)
            selectionClause = '{0} = {1}'.format('YEAR', year) + str(" AND ") + '{0} = '"'{1}'"''.format('AGENCY_GEAR',gear)
            arcpy.SelectLayerByAttribute_management(new_layer, "NEW_SELECTION", selectionClause)
            out_feat = spidentityOut + str(year) + str(gear)  
            arcpy.Identity_analysis(trawlBuffs, fishnet, out_feat)
            print("Writing Data File: " + out_feat)
            # Drop unnecessary Fields
            dropFields = ['FID_vms_net1k']
            arcpy.DeleteField_management(out_feat, dropFields)
except Exception as e:
    #dump errors
    print("Error: " + e.args[0]) 
finally:
    #Clean up the temp layer
    arcpy.Delete_management(new_layer)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 15 Mar 2022 18:42:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-within-nested-loop-not-working-as/m-p/1153962#M64059</guid>
      <dc:creator>ssintelmann</dc:creator>
      <dc:date>2022-03-15T18:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Selection within Nested Loop Not Working as Expected</title>
      <link>https://community.esri.com/t5/python-questions/selection-within-nested-loop-not-working-as/m-p/1153980#M64060</link>
      <description>&lt;P&gt;Maybe I'm missing something but you've applied a selection to new_layer then not done anything with it after.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#arcpy.Identity_analysis(trawlBuffs, fishnet, out_feat)
#do you mean?
arcpy.Identity_analysis(new_layer, fishnet, out_feat)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 15 Mar 2022 19:16:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-within-nested-loop-not-working-as/m-p/1153980#M64060</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2022-03-15T19:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Selection within Nested Loop Not Working as Expected</title>
      <link>https://community.esri.com/t5/python-questions/selection-within-nested-loop-not-working-as/m-p/1153990#M64061</link>
      <description>&lt;P&gt;No, you are absolutely correct!&amp;nbsp; sigh. but relief, i need a walk now. Thanks yet again.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Mar 2022 19:29:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selection-within-nested-loop-not-working-as/m-p/1153990#M64061</guid>
      <dc:creator>ssintelmann</dc:creator>
      <dc:date>2022-03-15T19:29:47Z</dc:date>
    </item>
  </channel>
</rss>

