7

I am using ArcPy with ArcMap.

I need to remove duplicates from a field named "Intersecti"

enter image description here

Here is my code. I used list..set. It's not removing any duplicates.

import arcpy
duplicates = "G:\\xStreetNew\\Duplicates.shp"
duplicates_List = []
with arcpy.da.SearchCursor(duplicates, ['Intersecti']) as Cur:
 for row in Cur:
    duplicates_List.insert(0,row[0])
    duplicates_List = list(set(duplicates_List))
 print duplicates_List
1
  • If you want to change values, you need to use an UpdateCursor.
    – phloem
    Mar 22 '16 at 22:44
11

You need to:

  • Use an update cursor
  • Split the string into a list
  • Remove duplicates
  • Put the list back into a string
  • Assign the string to that row/field
  • Apply the update

Try this:

import arcpy
duplicates = "G:\\xStreetNew\\Duplicates.shp"

with arcpy.da.UpdateCursor(duplicates, ['Intersecti']) as cur:
    for row in cur:
        row[0] = ','.join(list(set(row[0].split(','))))
        cur.updateRow(row)

Or--for clarity-- you could replace the jumbled, multi-operation line, like this:

import arcpy
duplicates = "G:\\xStreetNew\\Duplicates.shp"

with arcpy.da.UpdateCursor(duplicates, ['Intersecti']) as cur:
    for row in cur:
        v = row[0]
        v = v.split(',')
        v = list(set(v))
        v = ','.join(v)
        row[0] = v
        cur.updateRow(row)
1
  • 1
    +1, FYI, there is no need to make v into a list. ','.join(v) works when v is still a set.
    – Fezter
    Mar 23 '16 at 0:31
8

@Tom's answer is great. However, I'll post how this can be achieved in the field calculator.

It's a simple function:

Codeblock:

def delDups(n):
  return ', '.join(set(n.split(', ')))

Expression:

delDups( !Intersecti!)

Screenshot:

enter image description here

1
  • 1
    This can also be done as a straight expression (no codeblock) using ', '.join( set( !Intersecti!.split(', ' )))
    – Midavalo
    Aug 26 '16 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.