Select to view content in your preferred language

Query with a list

171
1
11-15-2024 10:27 AM
CW-GIS
by
Regular Contributor

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. 

  • If instead of looping, how would I just get the entire list of BuildingKeys and put that in the query?
  • Would it be 'includes'?
0 Kudos
1 Reply
TonyAlmeida
Frequent Contributor

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.")