# Selecting first distinct values and associated geometry using python

I have a layer I have created and assigned unique IDs. I only have a standard Arc license and require only one of each of the rows with a unique ID. Is there any way to select these using python? Or a work around where I can label them and select from there? These items do not always have duplicate geometry.

An example of what I hope to achieve is below:

(I have added the ORDER_ column and populated manually purely as an example)

Summary Statistics is available at any license level. Enter FID, Min for the Statistics Field. For the Case Field, enter SECTIONLAB. The resulting table should contain the first record for each unique SECTIONLAB. You can join this table back to the original table.

• No statistics can be done on FID, which is a shame gis.stackexchange.com/questions/133161/… – FelixIP Oct 10 '16 at 18:39
• Just add a new Integer Column and Calculate it it from FID. – klewis Oct 10 '16 at 18:42
• I think you should update your answer – FelixIP Oct 10 '16 at 19:05
• I just tested Summary Statistics using a Shapefile, the Minimum FID worked fine. – klewis Oct 10 '16 at 19:54
• It works from the tool, it does not from table view. Ok perhaps it is clear in your answer – FelixIP Oct 10 '16 at 20:08

In python you could create an array and make a statement telling it to go through your list using arcpy.SearchCursor and check if the ID is already in the array. If it's not there, add it to the list, if it is, then do nothing and go onto the next record. Once it's completed this "check" you can tell it to select by attributes based on the values in your array. Be sure to use "Add to current selection".

• Alternatively, he could create an iterator to go through and automate the creation of his "ORDER_" field. And then select all rows with a value of "1" in that field. – Jvhowube Oct 10 '16 at 15:06

`collections.defaultdict` is perfect for this. If you use a counter, choosing the last would be trickier (not required in this instance, but potentially useful nonetheless).

``````from collections import defaultdict
import arcpy

ddict = defaultdict(list)

for key,oid in arcpy.da.SearchCursor("my fc", ["keyField", "OID@"]):
ddict[key].append(oid)

# Get the first distinct value. Use -1 to access the last
oids = sorted([v[0] for v in ddict.itervalues()])

oidField = arcpy.AddFieldDelimiters("my workspace", arcpy.Describe("my fc").oidFieldName)
sql = "{} IN ({})".format(oidField, ",".join(map(str, oids)))
``````

With the following table, `gridcode` is my keyfield:

``````| OBJECTID | gridcode |
|----------|----------|
| 1        | 6        |
| 2        | 6        |
| 3        | 5        |
| 4        | 5        |
| 5        | 6        |
| 6        | 6        |
| 7        | 5        |
| 8        | 1        |
| 9        | 5        |

>>> print(sql)
OBJECTID IN (1,3,8)
``````

And for the last distinct value:

``````oids = sorted([v[-1] for v in ddict.itervalues()])
>>> print(sql)
OBJECTID IN (6,8,9)
``````