<?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 Nesting a While Loop Within a For Loop using arcpy.da.UpdateCursor() in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236243#M66185</link>
    <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;I am attempting to use the arcpy.Geometry() objects .union() method to update records in a feature class that were appended but now need to be merged with an existing feature where appropriate (governed by a SQL expression). I am using the arcpy.da.UpdateCursor() to perform the updates. I am attempting to do something like the following, but I am getting an "AttributeError: 'Array' object has no attribute 'union'" error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.MakeFeatureLayer_management(zoning, 'zoning_lyr')

arcpy.management.SelectLayerByAttribute('zoning_lyr', "NEW_SELECTION", "Zoning = 'A'")

selectCount = int(arcpy.GetCount_management('zoning_lyr')[0])


if selectCount &amp;gt;= 2:

    zoningGeom = arcpy.CopyFeatures_management('zoning_lyr',arcpy.Geometry())


    edit.startEditing(False, False)

    edit.startOperation()

    N = 1
    with arcpy.da.UpdateCursor(zoning, "Shape@", "Zoning = 'A'") as cursor:
        for row in cursor:
            if N == selectCount:break
            while N &amp;lt; selectCount:
               row = row[0].union(zoningGeom[N])   # Fails here
               cursor.updateRow([row])
               N += 1

    del row, cursor


    edit.stopOperation()

    edit.stopEditing(True)


    arcpy.Delete_management('zoning_lyr')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code below works without error, except it doesn't run through the whole list of zoningGeom features before moving to the next zoning record in the for loop. Ideally I would have it run through the records in the zoningGeom geometry object, unioning each one to the zoning layer before running out of features to union and exiting both loops.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;N = 1
with arcpy.da.UpdateCursor(zoning, "Shape@", "Zoning = 'A'") as cursor:
    for row in cursor:
        if N == selectCount:break
        row = row[0].union(zoningGeom[N])
        cursor.updateRow([row])
        N += 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I am just missing something fundamental here. Hopefully, someone can swoop in and point me in the right direction or, better yet, offer a solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 Nov 2022 15:06:38 GMT</pubDate>
    <dc:creator>daveostrander</dc:creator>
    <dc:date>2022-11-30T15:06:38Z</dc:date>
    <item>
      <title>Nesting a While Loop Within a For Loop using arcpy.da.UpdateCursor()</title>
      <link>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236243#M66185</link>
      <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;I am attempting to use the arcpy.Geometry() objects .union() method to update records in a feature class that were appended but now need to be merged with an existing feature where appropriate (governed by a SQL expression). I am using the arcpy.da.UpdateCursor() to perform the updates. I am attempting to do something like the following, but I am getting an "AttributeError: 'Array' object has no attribute 'union'" error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.MakeFeatureLayer_management(zoning, 'zoning_lyr')

arcpy.management.SelectLayerByAttribute('zoning_lyr', "NEW_SELECTION", "Zoning = 'A'")

selectCount = int(arcpy.GetCount_management('zoning_lyr')[0])


if selectCount &amp;gt;= 2:

    zoningGeom = arcpy.CopyFeatures_management('zoning_lyr',arcpy.Geometry())


    edit.startEditing(False, False)

    edit.startOperation()

    N = 1
    with arcpy.da.UpdateCursor(zoning, "Shape@", "Zoning = 'A'") as cursor:
        for row in cursor:
            if N == selectCount:break
            while N &amp;lt; selectCount:
               row = row[0].union(zoningGeom[N])   # Fails here
               cursor.updateRow([row])
               N += 1

    del row, cursor


    edit.stopOperation()

    edit.stopEditing(True)


    arcpy.Delete_management('zoning_lyr')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code below works without error, except it doesn't run through the whole list of zoningGeom features before moving to the next zoning record in the for loop. Ideally I would have it run through the records in the zoningGeom geometry object, unioning each one to the zoning layer before running out of features to union and exiting both loops.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;N = 1
with arcpy.da.UpdateCursor(zoning, "Shape@", "Zoning = 'A'") as cursor:
    for row in cursor:
        if N == selectCount:break
        row = row[0].union(zoningGeom[N])
        cursor.updateRow([row])
        N += 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I am just missing something fundamental here. Hopefully, someone can swoop in and point me in the right direction or, better yet, offer a solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 15:06:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236243#M66185</guid>
      <dc:creator>daveostrander</dc:creator>
      <dc:date>2022-11-30T15:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: Nesting a While Loop Within a For Loop using arcpy.da.UpdateCursor()</title>
      <link>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236254#M66187</link>
      <description>&lt;P&gt;edit and format your code would be helpful&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 01:26:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236254#M66187</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-11-30T01:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: Nesting a While Loop Within a For Loop using arcpy.da.UpdateCursor()</title>
      <link>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236262#M66188</link>
      <description>&lt;P&gt;It doesn’t appear to be possible on mobile. I will update/correct &amp;nbsp;tomorrow. Thank you for the tip.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 02:22:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236262#M66188</guid>
      <dc:creator>daveostrander</dc:creator>
      <dc:date>2022-11-30T02:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Nesting a While Loop Within a For Loop using arcpy.da.UpdateCursor()</title>
      <link>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236278#M66189</link>
      <description>&lt;P&gt;Your first script fails because you forgot the index in the failing line. So you're trying to use the non-existing union method of an array (row) instead of the geometry (row[0]).&lt;/P&gt;&lt;P&gt;But the script would fail there anyway, because &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/copy-features.htm" target="_blank" rel="noopener"&gt;arcpy.management.CopyFeatures()&lt;/A&gt; does not what you think it does. You seem to want to extract the geometries, but this tool copies a feature class from one location to another.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You got the gist of what you need to do:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Extract the geometries&lt;/LI&gt;&lt;LI&gt;Iterate over the rows with an UpdateCursor&lt;OL&gt;&lt;LI&gt;union the current geometry with all extracted geometries&lt;/LI&gt;&lt;LI&gt;write it into the feature class&lt;/LI&gt;&lt;LI&gt;optionally: you probably should delete every row after the first one, so you don't get overlapping features.&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;zones = "TestPolygons"  # layer name or fc path
zone_name_field = "TextField"  # name of the field that determines the "sameness"
only_keep_first = True  # only keep the first row of each zone, delete the following rows
zone_names = ["A"]  # Names you want to union
#  You can also do it automatically for every zone name:
#zone_names = {r[0] for r in arcpy.da.SearchCursor(zones, [zone_name_field])}


for zone_name in zone_names:
    sql = f"{zone_name_field} = '{zone_name}'"
    # extract the geometries into a list
    zone_geometries = [r[0] for r in arcpy.da.SearchCursor(zones, ["SHAPE@"], sql)]
    first_row = True
    # iterate over the fc
    with arcpy.da.UpdateCursor(zones, ["SHAPE@"], sql) as cursor:
        for r in cursor:
            if first_row:
                # union the current geometry with every extracted geometry
                geo = r[0]
                for other_geo in zone_geometries:
                    geo = geo.union(other_geo)
                cursor.updateRow([geo])
                # delete the following rows if only_keep_first == True
                first_row = not only_keep_first
            else:
                cursor.deleteRow()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Before:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1669788424910.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/57196i8A292DC2EABD0B5A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_0-1669788424910.png" alt="JohannesLindner_0-1669788424910.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With only_keep_first == True:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1669788802251.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/57197iF66B885D2BCFAE3F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_1-1669788802251.png" alt="JohannesLindner_1-1669788802251.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With only_keep_first == False:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_2-1669789685199.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/57198i067A1D0439ED8066/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_2-1669789685199.png" alt="JohannesLindner_2-1669789685199.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 06:38:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236278#M66189</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-11-30T06:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: Nesting a While Loop Within a For Loop using arcpy.da.UpdateCursor()</title>
      <link>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236461#M66197</link>
      <description>&lt;P&gt;Not only the solution I was after but served on a silver platter!!!&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 17:55:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nesting-a-while-loop-within-a-for-loop-using-arcpy/m-p/1236461#M66197</guid>
      <dc:creator>daveostrander</dc:creator>
      <dc:date>2022-11-30T17:55:34Z</dc:date>
    </item>
  </channel>
</rss>

