Hello everyone,Very helpful thread. Thanks. One thing I changed in my for loop was to use "ADD_TO_SELECTION" rather than use the "NEW_SELECTION". In my case the for loop would overwrite each previous selection which of course is not the intended result. The "ADD_TO_SELECTION" parameter acts like "NEW_SELECTION" for the first iteration. Here is my code for future reference to anyone else having similar problems. I have three seperate lists because I use them elsewhere in my function and my table contains 48 records. Of the 48 records only 37 are matched in the table as per my criteria. The idea behind this chunk of code within my function is to create a temporary or one time lookup table based on an area of interest. My goal was to select attributes I needed and delete the rows I did not need to complete my table.
sList = ['BG','PP','IDFxh1','IDFxh1a','IDFxh2','IDFxh2a']
mLIST = ['IDFdk1','IDFdk1a','IDFdk2','IDFdk3','IDFunk','MS']
dList = ['ESSF','ICH','CWH']
mergeList = sList+mLIST+dList
# This is using python list comprehension to add the '%' wildcard to each item in my list
# because the attributes BG, PP, MS, ESSF, ICH, CWH contain up to 4 more characters
mergeList = [x[:1]=='%' and x or x+'%' for x in mergeList]
print "The merged lists look like this:\n " + str(mergeList)
arcpy.MakeTableView_management("becFreqTbl","tempBecTbl")
for list in mergeList:
print 'iterating through list item:', list
where_clause = "MAP_LABEL LIKE '"+list+"'"
arcpy.SelectLayerByAttribute_management("tempBecTbl","ADD_TO_SELECTION",where_clause)
newSel = int(arcpy.GetCount_management("tempBecTbl").getOutput(0))
print "The new selection yieled "+str(newSel)+" fields selected"
arcpy.SelectLayerByAttribute_management("tempBecTbl","SWITCH_SELECTION")
switchSel = int(arcpy.GetCount_management("tempBecTbl").getOutput(0))
print "The switch selection yielded "+str(switchSel)+" fields selected"
arcpy.DeleteRows_management("tempBecTbl")
print "Deleted "+str(switchSel)+" rows from table that did not match the list criteria"
The output looks like this:The merged lists look like this:
['BG%', 'PP%', 'IDFxh1%', 'IDFxh1a%', 'IDFxh2%', 'IDFxh2a%', 'IDFdk1%', 'IDFdk1a%', 'IDFdk2%', 'IDFdk3%', 'IDFunk%', 'MS%', 'ESSF%', 'ICH%', 'CWH%']
iterating through list item: BG%
iterating through list item: PP%
iterating through list item: IDFxh1%
iterating through list item: IDFxh1a%
iterating through list item: IDFxh2%
iterating through list item: IDFxh2a%
iterating through list item: IDFdk1%
iterating through list item: IDFdk1a%
iterating through list item: IDFdk2%
iterating through list item: IDFdk3%
iterating through list item: IDFunk%
iterating through list item: MS%
iterating through list item: ESSF%
iterating through list item: ICH%
iterating through list item: CWH%
The new selection yieled 37 fields selected
The switch selection yielded 11 fields selected
Deleted 11 rows from table that did not match the list criteria