2

I have two features classes that should have the exact same fields. There are over 500 fields, I am looking for a code that will compare the fields and identify if there are fields in either feature class that are not in the other.
I found this solution, Comparing multiple field values of two feature classes using ArcPy? but was hoping to not have to type out each field.

6
  • Do the rows within the fields need to be the same? Have you tried using ListFields and comparing the lists? More details: pro.arcgis.com/en/pro-app/latest/arcpy/functions/listfields.htm
    – Aaron
    Jan 14 at 15:30
  • Yes I have used ListFields but I was hoping to find something that loops through the feature classes and identifies difference in the 2 features classes. Jan 14 at 15:36
  • Dow the values contained within the fields need to be the same too, or just the field names?
    – Aaron
    Jan 14 at 15:59
  • There are no values in the fields, they are blank. Jan 14 at 16:22
  • Finally, do the field types have to be the same between the two?
    – Aaron
    Jan 14 at 16:48
9

List the fields using set comprehension, compare the sets:

import arcpy

fc1 = r'C:\GIS\data\Bakgrundskartor_LMV\Oversiktskartan.gdb\ak_riks'
fc2 = r'C:\GIS\data\Bakgrundskartor_LMV\Oversiktskartan.gdb\ak_riks_1'

fields1 = {f.name for f in arcpy.ListFields(fc1)}
fields2 = {f.name for f in arcpy.ListFields(fc2)}

print 'Fields in common: ', fields1.intersection(fields2)

if not fields1==fields2: #If they are not the same
    print 'only exist in fc1, and not in fc2: ', fields1.difference(fields2)
    print 'only exist in fc2, and not in fc1: ', fields2.difference(fields1)

enter image description here

4
  • I see you are using set() methods but is fields1 and fields2 not a dictionary because they are {}?
    – Hornbydd
    Jan 14 at 15:58
  • 1
    No sets also use {}. I guess python know it is a set if there are no key:values
    – BERA
    Jan 14 at 15:59
  • 1
    Ah OK! I've only ever created a set like mySet = set() never knew it could be {}, I find that confusing! But a good answer showing the power of sets.
    – Hornbydd
    Jan 14 at 16:01
  • 1
    Nice, clean approach to check if the field names and list sizes are the same. The OP is also looking to check if the fields are the same type. How about integrating a check using f.type?
    – Aaron
    Jan 14 at 17:15

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.