[GIS] Creating Where Clause from user input via Python list with ArcPy

arcmaparcpycursorquerysql

I am trying to perform a query based on values in a Python list. The where clause will then be used in an Update Cursor, so that the rows NOT equal to the values in the list will be deleted.

This is what the list will look like after it is passed into the script from the user (using a multivalue parameter in the script tool), and the semicolon delimited string has been split. This is just an example of many choices the user can make.

userSelectionList = ["40","77","95]
selectionQRY = "RTE_1_NBR <> 'userSelectionList'" # <-- I know this doesn't work

Hard coded, it would look like:

selectionQRY = "RTE_1_NBR <> '40' AND RTE_1_NBR <> '77' AND RTE_1_NBR <> '95'"

But the input and number of selection depends on the user's selection.

It should finally be something like this:

userSelectionList = ["40","77","95]
selectionQRY = "RTE_1_NBR <> 'userSelectionList'"

with arcpy.da.UpdateCursor(userI,["RTE_1_NBR"],selectionQRY) as userUC:
    for row in userUC:
        userUC.deleteRow()
del userUC

Is it possible to make the where clause from a user input/dynamic Python list?

Best Answer

You can use the join method:

selectionQRY = "RTE_1_NBR <> '" + "' AND RTE_1_NBR <> '".join(userSelectionList) + "'"

A string will be created, in three parts. The middle part is the key, where code of the form string.join(list) will take all objects in the list, convert them to strings, and then append them all together, with the specified string interjected between each of them. So, ",".join(["a","b","c"]) would produce "a,b,c".

In your particular example, the code "' AND RTE_1_NBR <> '".join(userSelectionList) would produce the string 40' AND RTE_1_NBR <> '77' AND RTE_1_NBR <> '95 for userSelectionList = ["40","77","95"]. Obviously, the opening RTE_1_NBR <> ' and closing ' are missing from the query, so those need to be added separately, at the beginning and end, respectively.