Compare multiple lists of field names thought to be in shapefile with multiple lists of fields found in shapefile

01-28-2020 07:20 PM
by Anonymous User
Not applicable

In the code below, I am able to create and print multiple lists that contain field names for each shapefile. This is done by creating a list of file paths, pointing to a shapefile, and looping through the list passing each shapefile into the arcpy.ListFields function.

import arcpy

#Define the Workspace
arcpy.env.workspace = r"C:\TestFolder"

dbf1 = "APExistOnlyEdit.dbf"
myfield = "FI_PATH"

filepathlist =[row[0] for row in arcpy.da.SearchCursor(dbf1,myfield)]


#Loop through shapefiles in fielpathlist
#Get field names for each shapefile and pass into lists
for thisFile in filepathlist:
    ContainedFieldNames = [ for f in arcpy.ListFields(thisFile)]

#Fields with Field Names thought to be in Shapefile
#Search though Fields and rows and make lists of field names though to be in shapefile 

with arcpy.da.SearchCursor(dbf1,possiblefieldlist) as cursor:
    for row in cursor:    
        fields = [a.upper() for a in row if a.strip() != '']

Below is the output when I print the list of field names found in each shapefile. There are three separate lists, one for each shapefile. 




In addition, I use the arcpy.da.SearchCursor to loop through multiple fields, in a dbf file that contain names of fields, that are thought to be within each shapefile. I stripped out rows that do not contain any text. Below is the output when I print the lists of field names thought to be within the shapefile.




My goal with this script is to compare the lists of field names though to be within the shapefile (possible fields) to the lists of field names actually found within the shapefile (actual fields). For each comparison of lists, I want to capture the text of field names that are found and field names that are not found. I would like to use arcpy.da.UpdateCursor to fill two new fields, one field will hold the field names that are found and one will hold the field names that are not found. I am using a dbf table to control processing and it holds all field names thought to be within the shapefile and file paths to shapefiles and I want to write results to this dbf table as well. All of the examples that I have seen compare a single shapefile that is defined against one defined list. I am wondering if it's possible to compare multiple lists with multiple lists using ArcPy functions.

Below is a pic of my dbf table and I'm using ArcGIS Pro.

I assume that I need to do some list processing and get the difference or intersection of lists, but I cant wrap my head around the code logic. I'm new to the python language so any help to point me in the right direction would be greatly appreciated.

0 Kudos
1 Reply
Occasional Contributor III

Hey Jordan Garrison‌,

Would python sets work for you?

Taking your first two lists:



If I use sets I can see the matching items between the two lists:


The above outputs my matching list items as: set(['ZIPCODE'])

To go the other way and see all the ones that don't match you can use:


This will output the following using the above lists:




If this answer has helpful please mark it as helpful. If this answer solved your question please mark it as the answer to help others who have the same question.