I have features in a class fc.
I want to take the list of BuildingKeys and run a query to delete those from a different feature hosted_feature:
with arcpy.da.SearchCursor(fc, [BuildingKey]) as cursor:
for row in cursor:
variable = "'" + str(row[0]) + "'"
buildingkey = "BuildingKey = "
query = buildingkey + variable
deleted = hosted_feature.delete_features(where = query)
Right now it loops through and deletes each one individually.
maybe something like this,
import arcpy
# Collect all BuildingKeys into list, excluding empty, blank, or null values
building_keys = []
with arcpy.da.SearchCursor(fc, ["BuildingKey"]) as cursor:
for row in cursor:
building_key = row[0]
if building_key and str(building_key).strip(): # Check for non-empty, non-null, non-blank values
building_keys.append("'" + str(building_key) + "'") #building_keys.append(str(row[0]))
# Only proceed if there are valid BuildingKeys
if building_keys:
# Create a query string using IN
query = "BuildingKey IN ({})".format(", ".join(building_keys))
# Delete features using the query
deleted = hosted_feature.delete_features(where=query)
else:
print("No valid BuildingKeys to delete.")