I would like to create a subset of a table based on values in a Python list that I created from a search cursor. I have not been able to get the where_clause correct and keep getting errors. Here is my code:
pu = 'PlanningUnits'
hab = 'lu_Habitat'
with arcpy.da.SearchCursor(pu, "unique_id") as cursor:
selected_pu = sorted({row[0] for row in cursor})
where_clause = """{} IN selected_pu""".format(arcpy.AddFieldDelimiters(hab, "unique_id"))
selected_hab = arcpy.TableToTable_conversion(hab, r'in_memory', "selected_hab", where_clause)
I have also tried the following as a where_clause:
where_clause = """{} IN """.format(arcpy.AddFieldDelimiters(hab, "unique_id")) + str(tuple(selected_pu))
In both cases, I am getting the following error:
ExecuteError: ERROR 999999: Error executing function. An invalid SQL statement was used. An invalid SQL statement was used.