I have a table. Based of field name "TYP", I would like to select rows which contains string values "a" or "b". I need to specify the query to my arcpy code.

fc = "C:\\Users\\type_jan18.shp"
field = "TYP"
val1 = "a"
val2 = "b"

I understand how to build expression if I have one condition:

# select by one condition
whereClause = '"TYP"' + " = '" + str(val1) + "'"

# by using FieldDelimiters, again one condition
whereClause = """{} = str(val1) """.format(arcpy.AddFieldDelimiters(fc, field))

But how to correctly state it with the OR statement?

This one seems not working...

  whereClause = field + " = '" + val1 + "'"+ "or"  + field + " = '" + val2 + "'"

1 Answer 1


Dont use +-signs, instead use format like you did with one condition. Format can take more arguments than two:

sql = """{0}='{1}' OR {0}='{2}'""".format(arcpy.AddFieldDelimiters(fc,  field), val1 ,val2)

Which will give the same results as:

sql = """{0} IN('{1}', '{2}')""".format(arcpy.AddFieldDelimiters(fc,  field), val1, val2)

And if you have more variables than format can handle you can place them inside a tuple:

sql = """{0} IN{1}""".format(arcpy.AddFieldDelimiters(fc,  field), (val1, val2,...,val9999))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.