I'm having trouble deleting duplicates from multiple tables in a geodatabase. I have a field "facility_id" which should be unique, but I have multiple entries and need to delete all but one. I've started a python script to delete the duplicates but I'm not sure how to finish it. I tried a for loop to find all facility_ids in the values list and check for membership in the duplicate values list, but when I use cursor.deleteRow, nothing happens.
import arcpy, os
from arcpy import env
targetDB = r"C:\data\Temp\436e0250ef1748af92e7f1f75b5a0deb.gdb"
arcpy.env.workspace = targetDB
for table in arcpy.ListTables():
newTable = arcpy.Describe(table).basename
if "ATTACH" not in newTable:
with arcpy.da.UpdateCursor(newTable, "facility_id") as cursor:
values = [r[0] for r in cursor]
duplicates = [value for value in values if values.count(value) > 1]
print "Original Table Values: " + str(newTable) + ": " + str(values)
print "Duplicate Table Values: " + str(newTable) + ": " + str(duplicates)
if values in duplicates:
cursor.deleteRow()
output:
Original Table Values: ElecConduit: [u'PS145', u'PS022', u'PS022', u'PS108', u'PS134', u'PS999']
Duplicates Table Values: ElecConduit: [u'PS022', u'PS022']
Original Table Values: ElecLighting: [u'PS108', u'PS108', u'PS108']
Duplicates Table Values: ElecLighting: [u'PS108', u'PS108', u'PS108']
values
. Then you try to use it later to delete a row but you have already passed through the entire cursor. This is probably why your code is not working? Jim's code only ever passes through it once so its a better solution.