I simply want to create a script which compares a list with the field names of a featureclass and if the fields do not exist in the featureclass add them.
My code works, but is very inefficient. How can I compare lists in a more efficient manner?
import sys,os
import arcpy
from arcpy import env
env.overwriteOutput = True
workspace = r"PATH_TO_WORKSPACE"
fc = r"PATH_TO_FEATURECLASS"
lst_fcfields = arcpy.ListFields(fc)
lst_myfields = ["field1","field2"]
for field in lst_fcfields:
for myfield in lst_myfields:
if field.name ==myfield:
print fc +" - same as - "
else:
print field.name + " - not same as - " + myfield
arcpy.AddField_management(fc, myfield, "TEXT", "", "", 255)
Ideally, I would like the script to work interactively and ask the user if they would like to delete the fields which vary from the list, but as far as I know there are no dialogs in arcpy.
arcpy
question. python is full of wonderful objects like sets, lists, and dictionaries, so reading python documentation will help immensely with any arcpy code. just try searching "compare python lists" and you'll see!