<?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: Get next element of cursor arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/get-next-element-of-cursor-arcpy/m-p/654858#M50995</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;See this thread for ideas: &lt;A _jive_internal="true" href="https://community.esri.com/thread/180514"&gt;Compare value from n and n+1 row in searchCursor&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 24 May 2017 00:31:22 GMT</pubDate>
    <dc:creator>RandyBurton</dc:creator>
    <dc:date>2017-05-24T00:31:22Z</dc:date>
    <item>
      <title>Get next element of cursor arcpy</title>
      <link>https://community.esri.com/t5/python-questions/get-next-element-of-cursor-arcpy/m-p/654857#M50994</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to compare two values, and can't seem to access the next row in a cursor.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for i, row in enumerate(cursor):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if row[0] == cursor.next()[0]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 May 2017 22:59:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-next-element-of-cursor-arcpy/m-p/654857#M50994</guid>
      <dc:creator>KyleDunaway1</dc:creator>
      <dc:date>2017-05-23T22:59:01Z</dc:date>
    </item>
    <item>
      <title>Re: Get next element of cursor arcpy</title>
      <link>https://community.esri.com/t5/python-questions/get-next-element-of-cursor-arcpy/m-p/654858#M50995</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;See this thread for ideas: &lt;A _jive_internal="true" href="https://community.esri.com/thread/180514"&gt;Compare value from n and n+1 row in searchCursor&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 May 2017 00:31:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-next-element-of-cursor-arcpy/m-p/654858#M50995</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2017-05-24T00:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Get next element of cursor arcpy</title>
      <link>https://community.esri.com/t5/python-questions/get-next-element-of-cursor-arcpy/m-p/654859#M50996</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Beyond reading through the thread recommended by &lt;A href="https://community.esri.com/migrated-users/34565" target="_blank"&gt;Randy Burton&lt;/A&gt;‌, a couple of questions:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;What type of cursor(s) are you working with?&lt;/LI&gt;&lt;LI&gt;What happens if the condition is satisfied?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE&lt;/STRONG&gt;:&amp;nbsp; continued....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As mentioned in the referenced GeoNet discussion, ArcPy cursors do not implement any kind of look- or read-ahead feature.&amp;nbsp; If one is working solely within DBMSs, there are likely set-based approaches that are better then using cursors for look-ahead functionality, .e.,g &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;LAG&lt;/SPAN&gt; and &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;LEAD&lt;/SPAN&gt; functionality implemented in many DBMSs; but one needs to work with what is available to them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are multiple ways to emulate look- or read-ahead functionality with Python and ArcPy, several are covered in the aforementioned GeoNet discussion.&amp;nbsp; Since I commented on that discussion last year, I have refined the approach I put forward.&amp;nbsp; Functionally, the approach below is not any different, but syntactically it is much more streamlined, I would argue more Pythonic.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; itertools &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; izip_longest

fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN class="comment token"&gt;# path to feature class&lt;/SPAN&gt;
flds &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# string list of field names for cursors&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; flds&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; n1_cur&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;UpdateCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; flds&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; n_cur&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; filler_row &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; next&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;n1_cur&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; n_row&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; n1_row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; izip_longest&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;n_cur&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; n1_cur&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fillvalue&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;filler_row&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; n_row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;==&lt;/SPAN&gt; n1_row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n_cur&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;updateRow&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The basic approach is to pair a search cursor with an update cursor.&amp;nbsp; The search cursor is used for the look ahead (n+1 cursor or &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;n1_cur&lt;/SPAN&gt;) while the update cursor is used for the current record (n cursor or &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;n_cur&lt;/SPAN&gt;).&amp;nbsp; The &lt;A href="https://docs.python.org/2/library/itertools.html#itertools.izip_longest" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;SPAN style="font-family: courier new,courier,monospace;"&gt;itertools.izip_longest()&lt;/SPAN&gt;&lt;/A&gt; function is used to pair the two cursors so they can be stepped through together, with the search cursor being one record ahead of the update cursor.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE 2&lt;/STRONG&gt;:&amp;nbsp; Removed &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;empty_row&lt;/SPAN&gt; and created &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;filler_row&lt;/SPAN&gt; since previous code for &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;empty_row&lt;/SPAN&gt; would fail with &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;datetime&lt;/SPAN&gt; or &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;geometry&lt;/SPAN&gt; fields.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:44:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-next-element-of-cursor-arcpy/m-p/654859#M50996</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-12T03:44:39Z</dc:date>
    </item>
  </channel>
</rss>

