<?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: Returning the first unique result with searchcursor? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40224#M3221</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hard to really interpret what you are asking for, but if you really want to find the "next" unique value in a field, here is one method:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy

fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'C:\Users\xxxxx\Desktop\tmp.gdb\cc4'&lt;/SPAN&gt;
fields &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'_type'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# load all values from the listed field into List with SearchCursor&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# then, makes a set from the list (this gives only unique values)&lt;/SPAN&gt;

Uni &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; set&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;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="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&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; fields&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="comment token"&gt;# since we now have a set with the unique values only, you can&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# iterate through that set now and do as you plese with each one&lt;/SPAN&gt;



&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; val &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; Uni&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Do something with this value&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# or, write to text file&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# or, add to dictionary&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# etc.&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;/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;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:34:08 GMT</pubDate>
    <dc:creator>RhettZufelt</dc:creator>
    <dc:date>2021-12-10T21:34:08Z</dc:date>
    <item>
      <title>Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40223#M3220</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Looking for a way to return the next unique result with a searchcursor in arcpy.&lt;/P&gt;&lt;P&gt;The table is from a spatial join of areas and has been sorted through python already.&lt;/P&gt;&lt;P&gt;However, I would like a way to have the name of the result recorded to a text file (or other dictionary) and to only take the next result each time the script runs and to check the dictionary to determine the next row to take.&lt;/P&gt;&lt;P&gt;Therefore allowing me to work row by row, one at a time, where they will be exported to a GeoJSON file through quick export.&lt;/P&gt;&lt;P&gt;I am struggling to find a way to make this executable.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Mar 2019 23:16:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40223#M3220</guid>
      <dc:creator>BenjaminBrewer</dc:creator>
      <dc:date>2019-03-26T23:16:20Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40224#M3221</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hard to really interpret what you are asking for, but if you really want to find the "next" unique value in a field, here is one method:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy

fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'C:\Users\xxxxx\Desktop\tmp.gdb\cc4'&lt;/SPAN&gt;
fields &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'_type'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# load all values from the listed field into List with SearchCursor&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# then, makes a set from the list (this gives only unique values)&lt;/SPAN&gt;

Uni &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; set&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;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="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&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; fields&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="comment token"&gt;# since we now have a set with the unique values only, you can&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# iterate through that set now and do as you plese with each one&lt;/SPAN&gt;



&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; val &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; Uni&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Do something with this value&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# or, write to text file&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# or, add to dictionary&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# etc.&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;/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;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:34:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40224#M3221</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-10T21:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40225#M3222</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Python sets are unordered, so the posted code snippet will provide a list of unique values for a field(s) but it won't provide the "next unique value."&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Mar 2019 17:08:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40225#M3222</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2019-03-27T17:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40226#M3223</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thus the comment about not being clear about what is being asked for.&lt;/P&gt;&lt;P&gt;Guess I'm not sure what the '&lt;SPAN style="background-color: #ffffff;"&gt;next unique result' is.&amp;nbsp; Is that the next, non-equal consecutive value in the sorted table?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;the&amp;nbsp;first time a value is encountered in the sorted table, etc?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;OP mentions wanting to use the next result 'each time' and work row by row (assuming across the entire table).&lt;/P&gt;&lt;P&gt;Based on the vagueness of the question, it sounded like OP wanted to be able to perform export operations for rows with a specified value in a particular field. A set would give all the unique values loaded from that field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Iterating through the set will give you the next value, then the next,&amp;nbsp; etc. of the set itself (not necessarily the order they were in the original table), but would give a list of unique values to query on......&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;of course, this could all be mute depending on the actual question.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Mar 2019 19:01:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40226#M3223</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2019-03-27T19:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40227#M3224</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/350455"&gt;Benjamin Brewer&lt;/A&gt;‌, as the discussion between &lt;A href="https://community.esri.com/migrated-users/11604"&gt;Rhett Zufelt&lt;/A&gt;‌ and I shows, there are multiple ways to interpret what you are trying to accomplish.&amp;nbsp; What would be most helpful is to provide either an actual or made-up example that you and commenters can respond to directly.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Mar 2019 19:07:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40227#M3224</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2019-03-27T19:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40228#M3225</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Joshua and &lt;A href="https://community.esri.com/migrated-users/11604"&gt;Rhett Zufelt&lt;/A&gt;‌, sorry I have had a really busy day in school. Thank you for looking over at my question.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To clarify some more of what it is I am trying to do.&lt;/P&gt;&lt;P&gt;I have a feature class, named TWP_Messages which is a spatial join of GPS points and the ATS grid system in Alberta.&lt;/P&gt;&lt;P&gt;I have sorted this feature class by a count field for the amount of GPS messages in each township. I then want to take the top result of that join and export it into a GeoJSON file which I will then use, incorporated with a script I have written to download satellite imagery from Planet Labs. I however only want to do this one township (1 cell of the ATS grid) at a time so not to be overloaded.&lt;/P&gt;&lt;P&gt;To achieve this I was hoping to find a way that a search cursor, or similar will take the first area from the spatial join, but as it exports this area it writes the area name in to a text file. I then ideally would like that text file to be used as a check list so when I next run the script it will then look at the text file, know that it has taken the top ranked area from the sorted join feature class and then move on to the next area in the list.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Spatial join between townships and GPS messages" class="image-1 jive-image" height="557" src="https://community.esri.com/legacyfs/online/440575_TWP_Messages.JPG" width="757" /&gt;&lt;/P&gt;&lt;P&gt;So in this feature class the first time it runs it will select the row for object ID 1520, the second time the script runs I would like for it to be able to check which areas have already been selected, so the first row in the table, ignore that and then move on to the second row so it can be exported.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope that is easier to understand. I am sure it can be done but I cant seem to find a similar instance where something similar has been done before.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Again, thank you for all your help. I hope I have described the situation better.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Mar 2019 23:46:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40228#M3225</guid>
      <dc:creator>BenjaminBrewer</dc:creator>
      <dc:date>2019-03-27T23:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40229#M3226</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could use the code of &lt;SPAN class=""&gt;Rhett Zufelt and go one with writing every unique value to a list and use the max of the list to find the highest count.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; val &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; Uni&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;

    MyList&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;append&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;val&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

MaxValue &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; max&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;MyList&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Max value element : "&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; MaxValue‍‍‍‍‍‍&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;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE&gt;&lt;SPAN class=""&gt;After that you can use MaxValue to do a select by attribute on your featureclass which I would copy before.&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE&gt;&lt;SPAN class=""&gt;Let you give out the name of the selected row with another search cursor&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE&gt;&lt;SPAN class=""&gt;for row in arcpy.SearchCursor(fc_Layer):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.FILE_NAME&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE&gt;&lt;SPAN class=""&gt;Write this to a text file&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE&gt;&lt;SPAN class=""&gt;Delete the selected feature and go to the next MaxValue&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE&gt;&lt;SPAN class=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:34:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40229#M3226</guid>
      <dc:creator>JohannesBierer</dc:creator>
      <dc:date>2021-12-10T21:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40230#M3227</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The feature class is static, right?&amp;nbsp; That is, the results of the spatial join aren't changing between times you extract the result for a new township.&amp;nbsp; If the spatial join is static, you really want to process the whole feature class once, create a single output file with the results, and then go through that file one at a time.&amp;nbsp; Trying to implement the specific steps you describe complicates the scripting and would perform poorly since you are constantly going back to the feature class.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This type of question gets asked every 6 months or so on GeoNet.&amp;nbsp; &lt;A href="https://community.esri.com/thread/199381" target="_blank"&gt;GroupBy 2 columns and keep all fields&lt;/A&gt;&amp;nbsp; has a good discussion of different approaches using ArcPy cursors and NumPy.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Adapted from one of the code snippets in the thread mentioned above, the following creates a layer and selects records.&amp;nbsp; After the records are selected, you can export them however you like.&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; groupby
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; operator &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; itemgetter

fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# path to feature class&lt;/SPAN&gt;
case_fields &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"DESCRIPTOR"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
max_field &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Count_"&lt;/SPAN&gt;
sql_orderby &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"ORDER BY {}, {} DESC"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;", "&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;join&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;case_fields&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; max_field&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

fl &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;MakeFeatureLayer_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"fc_lyr"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;getOutput&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;
oidField &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;Describe&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fl&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;OIDFieldName
oids &lt;SPAN class="operator 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="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;fl&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"*"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; sql_clause&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;None&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; sql_orderby&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; cur&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    oidField &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; cur&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;fields&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;index&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;oidField&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    case_func &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; itemgetter&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;
        &lt;SPAN class="operator token"&gt;*&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;cur&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;fields&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;index&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fld&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; fld &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; case_fields&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; key&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; group &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; groupby&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;cur&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; case_func&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        oids&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;append&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;next&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;group&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;oidField&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        &lt;SPAN class="keyword token"&gt;continue&lt;/SPAN&gt;

fl&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;setSelectionSet&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"NEW_SELECTION"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; oids&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;/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;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:34:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40230#M3227</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-10T21:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40231#M3228</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I had not thought about deleting data as I wanted to keep the original dataset complete but this could be the option I need. So with the print row.FILE_NAME I can take the selected area and then export it as I wish, then as it deletes that row from the table the next time it runs it will just start from the next available row. Thank you, I will give this a try.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Mar 2019 17:50:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40231#M3228</guid>
      <dc:creator>BenjaminBrewer</dc:creator>
      <dc:date>2019-03-28T17:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the first unique result with searchcursor?</title>
      <link>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40232#M3229</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Joshua, yes the join is static once it has been completed. In the real world scenario this may not be the case but from what I want to achieve here then it is more than appropriate. I had thought I was going a bit over complicated and that is why I was struggling to find examples. I will be trying to implement this further, thank you for the link you provided too, gives me more to read up on and work out from there.&lt;/P&gt;&lt;P&gt;Thank you for your help. I will update with what is working best once I have adapted the changes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Mar 2019 17:54:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/returning-the-first-unique-result-with/m-p/40232#M3229</guid>
      <dc:creator>BenjaminBrewer</dc:creator>
      <dc:date>2019-03-28T17:54:09Z</dc:date>
    </item>
  </channel>
</rss>

