<?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: How to get a count zero for arcpy.GetCount_management in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1229866#M66047</link>
    <description>&lt;P&gt;Store the count in a variable. If you encounter your break condition, change that variable to 0 before breaking out of the for loop.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for row in cursor:
    cenPnt = row[1]
    
    arcpy.management.SelectLayerByLocation(ssLyr, 'INTERSECT', cenPnt)
    
    ssLyrCount = int(arcpy.GetCount_management(ssLyr).getOutput(0))

    while ssLyrCount &amp;gt; 0:
        
        with arcpy.da.SearchCursor(ssLyr, fieldName2) as cursor1:
            
            for row1 in cursor1:
                biorID = row1[2]
                neigbID = row1[3]
                
                if neigbID == 9999:
                    print("\nError - Exit loop")
                    ssLyrCount = 0
                    break&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know the rest of your script, but the snippet you posted will result in an infinite while loop if you select at least 1 feature and have no features with neigbID=9999 in the selection. Any reason to use &lt;STRONG&gt;while&lt;/STRONG&gt; over &lt;STRONG&gt;if&lt;/STRONG&gt;?&lt;/P&gt;</description>
    <pubDate>Wed, 09 Nov 2022 09:32:43 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-11-09T09:32:43Z</dc:date>
    <item>
      <title>How to get a count zero for arcpy.GetCount_management</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1229840#M66045</link>
      <description>&lt;P&gt;Hello, I am just wondering if anyone tells me the way to get a count zero to exit from the while loop that uses arcpy.GetCount_management.&lt;/P&gt;&lt;P&gt;I set up for loop to go through a selection of intersection between polygon (ssLyr) layer and point geometry (cenPnt). Then, while the selected polygon count is greater than zero, a second for loop is carried out. But I would like to exit the while loop in case a specific value (neigbID == 9999) is returned during the second loop.&lt;/P&gt;&lt;P&gt;I tried a couple of ways; arcpy.management.SelectLayerByAttribute(ssLyr, "CLEAR_SELECTION"), del cursor1,&amp;nbsp;arcpy.Delete_management(ssLyr) to get zero count from&amp;nbsp;int(arcpy.GetCount_management(ssLyr).getOutput(0))&amp;nbsp; but none of them worked. Thank you in advance for any python coding help!&lt;/P&gt;&lt;P&gt;My code is here&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for row in cursor:
    cenPnt = row[1]
    
    arcpy.management.SelectLayerByLocation(ssLyr, 'INTERSECT', cenPnt)
    
    while int(arcpy.GetCount_management(ssLyr).getOutput(0)) &amp;gt; 0:
        
        with arcpy.da.SearchCursor(ssLyr, fieldName2) as cursor1:
            
            for row1 in cursor1:
                biorID = row1[2]
                neigbID = row1[3]
                
                if neigbID == 9999:
                    print("\nError - Exit loop")
                    #Would like to get count zero here
                    #arcpy.management.SelectLayerByAttribute(ssLyr, "CLEAR_SELECTION")
                    #del cursor1
                    #arcpy.Delete_management(ssLyr)
                    print("Check count: ", int(arcpy.GetCount_management(ssLyr).getOutput(0))) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 08:23:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1229840#M66045</guid>
      <dc:creator>Aнастасия88</dc:creator>
      <dc:date>2022-11-09T08:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a count zero for arcpy.GetCount_management</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1229861#M66046</link>
      <description>&lt;P&gt;The first getcount_management needs to return a value needs to return a value when the script ends.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 09:29:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1229861#M66046</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-11-09T09:29:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a count zero for arcpy.GetCount_management</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1229866#M66047</link>
      <description>&lt;P&gt;Store the count in a variable. If you encounter your break condition, change that variable to 0 before breaking out of the for loop.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for row in cursor:
    cenPnt = row[1]
    
    arcpy.management.SelectLayerByLocation(ssLyr, 'INTERSECT', cenPnt)
    
    ssLyrCount = int(arcpy.GetCount_management(ssLyr).getOutput(0))

    while ssLyrCount &amp;gt; 0:
        
        with arcpy.da.SearchCursor(ssLyr, fieldName2) as cursor1:
            
            for row1 in cursor1:
                biorID = row1[2]
                neigbID = row1[3]
                
                if neigbID == 9999:
                    print("\nError - Exit loop")
                    ssLyrCount = 0
                    break&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know the rest of your script, but the snippet you posted will result in an infinite while loop if you select at least 1 feature and have no features with neigbID=9999 in the selection. Any reason to use &lt;STRONG&gt;while&lt;/STRONG&gt; over &lt;STRONG&gt;if&lt;/STRONG&gt;?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 09:32:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1229866#M66047</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-11-09T09:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a count zero for arcpy.GetCount_management</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1230241#M66050</link>
      <description>&lt;P&gt;Hi Johannes, thanks a lot for your follow up and inquiry!&lt;/P&gt;&lt;P&gt;The rest of scripts continues with elif to deal with a case that biorID is not equal to neighbID. This continues while the output count is one but once it gets zero, then other functions are to be triggered. I don't know if this is the best way but I wrote a code like below.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;elif biorID != neighbID:

    Expression2 = "BID = {0}".format(neighbID)
    arcpy.management.SelectLayerByAttribute(ssLyr, 'NEW_SELECTION', Expression2)
    print('Expression2 is: ', Expression2)

    if ssLyrCount == 0:
        ##Other functions##
   
    else:
        pass&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 22:05:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1230241#M66050</guid>
      <dc:creator>Aнастасия88</dc:creator>
      <dc:date>2022-11-09T22:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a count zero for arcpy.GetCount_management</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1230262#M66052</link>
      <description>&lt;P&gt;Hi Dan, thanks for your reply!&lt;/P&gt;&lt;P&gt;What I wanted is actually just keep the while loop continues in the special case.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 22:33:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-a-count-zero-for-arcpy-getcount/m-p/1230262#M66052</guid>
      <dc:creator>Aнастасия88</dc:creator>
      <dc:date>2022-11-09T22:33:39Z</dc:date>
    </item>
  </channel>
</rss>

