<?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 Enumeration of a cursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524050#M41099</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Xander, I recently unlocked the mysteries of &lt;A href="https://docs.python.org/2/library/functions.html#enumerate" rel="nofollow noopener noreferrer" target="_blank"&gt;enumeration&lt;/A&gt;​ and noticed &lt;A _jive_internal="true" href="https://community.esri.com/message/492173#492173" target="_blank"&gt;your &lt;SPAN style="font-family: 'courier new', courier;"&gt;cnt&lt;/SPAN&gt; variable&lt;/A&gt; does something similar. Because I'm still learning, I thought something like this would work instead:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.SearchCursor(fc_house, flds_house) as curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for cnt, row_in in enumerate([i for i in curs_in], start=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_in.reset()&amp;nbsp; ## Return cursor back to the first row after enumeration
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 25 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing connection: {0}".format(cnt)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pnt1 = row_in[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parcel_id = row_in[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date_sale = row_in[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fire_oid = row_in[3]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Continue processing rows in cursor...&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After some tinkering, I realized that when you enumerate a cursor like this, it goes through all the rows. The problem is that the cursor object ends with no more rows and you have to call &lt;SPAN style="font-family: 'courier new', courier;"&gt;reset()&lt;/SPAN&gt; on the cursor to start it back at the first row again. Since there is still more processing to be done, you'll essentially be iterating over all rows in the cursor twice with enumeration instead of once with your original counter variable. Do you think the extra time it takes to create the enumeration of the cursor is worth it in a case like this? Maybe only for cursors with a small number of rows?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 22:50:45 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2021-12-11T22:50:45Z</dc:date>
    <item>
      <title>Enumeration of a cursor</title>
      <link>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524050#M41099</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Xander, I recently unlocked the mysteries of &lt;A href="https://docs.python.org/2/library/functions.html#enumerate" rel="nofollow noopener noreferrer" target="_blank"&gt;enumeration&lt;/A&gt;​ and noticed &lt;A _jive_internal="true" href="https://community.esri.com/message/492173#492173" target="_blank"&gt;your &lt;SPAN style="font-family: 'courier new', courier;"&gt;cnt&lt;/SPAN&gt; variable&lt;/A&gt; does something similar. Because I'm still learning, I thought something like this would work instead:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.SearchCursor(fc_house, flds_house) as curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for cnt, row_in in enumerate([i for i in curs_in], start=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_in.reset()&amp;nbsp; ## Return cursor back to the first row after enumeration
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 25 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing connection: {0}".format(cnt)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pnt1 = row_in[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parcel_id = row_in[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date_sale = row_in[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fire_oid = row_in[3]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Continue processing rows in cursor...&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After some tinkering, I realized that when you enumerate a cursor like this, it goes through all the rows. The problem is that the cursor object ends with no more rows and you have to call &lt;SPAN style="font-family: 'courier new', courier;"&gt;reset()&lt;/SPAN&gt; on the cursor to start it back at the first row again. Since there is still more processing to be done, you'll essentially be iterating over all rows in the cursor twice with enumeration instead of once with your original counter variable. Do you think the extra time it takes to create the enumeration of the cursor is worth it in a case like this? Maybe only for cursors with a small number of rows?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:50:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524050#M41099</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-12-11T22:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Enumeration of a cursor</title>
      <link>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524051#M41100</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Blake, interesting question... &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have branched it to a new thread instead of keeping it as a remark of the thread &lt;A href="https://community.esri.com/thread/140989"&gt;add time constraint to near tool&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am not sure what the benefit would be of using enumeration. If I loop through the cursor using the standard method (for row in cursor) and keep track of a counter, that is little overhead, I think...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Maybe, &lt;A href="https://community.esri.com/migrated-users/3116"&gt;Dan Patterson&lt;/A&gt;​, &lt;A href="https://community.esri.com/migrated-users/3420"&gt;Joshua Bixby&lt;/A&gt; or &lt;A href="https://community.esri.com/migrated-users/8586"&gt;Jake Skinner&lt;/A&gt;​ want to jump in and give their opinion on this?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2015 18:31:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524051#M41100</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-09T18:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: Enumeration of a cursor</title>
      <link>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524052#M41101</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am not one of those Pythonista's that worries about time to execute...unless we are talking minutes versus seconds.&amp;nbsp; I address those issues in the following way:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;What IS the difference in processing times?&lt;/LI&gt;&lt;LI&gt;If two methods are offered, what is the easiest to conceptually understand and explain&lt;/LI&gt;&lt;LI&gt;as for the above, which "looks" better ( a classic example is some of the fancy indexing using numpy strides...works great...looks totally bizarre)&lt;/LI&gt;&lt;LI&gt;does one method facilitate the addition of complementary information at the same time if it is needed later&lt;/LI&gt;&lt;LI&gt;do both yield the same results...and this is the key...if they do, they you have added to your arsenal of tools&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;So in short, I am worried less about one versus the other but learning/presenting more ways to do the same thing&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2015 21:09:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524052#M41101</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-04-09T21:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: Enumeration of a cursor</title>
      <link>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524053#M41102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The &lt;SPAN style="font-family: courier new,courier;"&gt;enumerate&lt;/SPAN&gt; function isn't exhausting the cursor, at least not the first time, the list comprehension is doing it and requiring the cursor to be reset.&amp;nbsp; Try restructuring your code as follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.SearchCursor(fc_house, flds_house) as curs_in:
&amp;nbsp;&amp;nbsp; for cnt, row_in in enumerate(curs_in, start=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 25 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing connection: {0}".format(cnt)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pnt1 = row_in[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parcel_id = row_in[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date_sale = row_in[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fire_oid = row_in[3]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Continue processing rows in cursor...&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The &lt;SPAN style="font-family: courier new,courier;"&gt;enumerate&lt;/SPAN&gt; function operates against "a sequence, an iterator, or some other object which supports iteration."&amp;nbsp; The data access cursors support iteration, which is also what allows us to use them with a &lt;SPAN style="font-family: courier new,courier;"&gt;for&lt;/SPAN&gt; statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Whereas &lt;A href="https://docs.python.org/2/reference/expressions.html#generator-expressions" rel="nofollow noopener noreferrer" target="_blank"&gt;generator expressions &lt;/A&gt;are evaluated lazily, comprehensions are not, which means your list comprehension needs to fully iterate over the cursor before returning any and all values in a list.&amp;nbsp; It is for this reason you are having to reset your cursor before doing any processing.&amp;nbsp; Since cursors already support iteration, just pass them directly to &lt;SPAN style="font-family: courier new,courier;"&gt;enumerate&lt;/SPAN&gt; and don't bother building a list of the entire cursor.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:50:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524053#M41102</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T22:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Enumeration of a cursor</title>
      <link>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524054#M41103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nailed it. Thanks Joshua!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 15:21:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/enumeration-of-a-cursor/m-p/524054#M41103</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2015-04-13T15:21:20Z</dc:date>
    </item>
  </channel>
</rss>

