Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
    
If you want to change values, you need to use an UpdateCursor. – phloem Mar 22 at 22:44
up vote 8 down vote accepted

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)
share|improve this answer
    
+1, FYI, there is no need to make v into a list. ','.join(v) works when v is still a set. – Fezter Mar 23 at 0:31

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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