Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to cursor search through items in a field in a feature class. I then append them to a list called 'distList'. I want to pass the list into layers definition query as follows:

"Field1" in distList

or in long hand, it would look like this:

"Field1" in ('ds(c1)', 'ds(b1)', 'ds(c2)', 'ds(g1)')

The list looks like this:

['ds(c1)', 'ds(b1)', 'ds(c2)', 'ds(g1)']

The problem I am having is, the definition query above doesn't like to look 'in' a python list. It doesn't recognize the square brackets. It prefers round brackets, so I thought about using a tuple. The problem I am facing is I don't know how to build a tuple after cursor searching the items from the feature class. here's my code example:

distList = []
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if == "Disturbance":
           for row in arcpy.SearchCursor(lyr):
                if "ds" in row.Field1:
    lyr.definitionQuery = '"Field1"' + "in " + distList

Can anyone suggest a way to get my list into a tuple or maybe just a better way to get my items into a format where they have round brackets instead of square ones?

As a work around, I tried converting the list to string str(distList) and then replacing the brackets. That works fine, but it seems cumbersome and I'm sure there's a better way.

Thanks, Mike

share|improve this question
up vote 5 down vote accepted

The braces/brackets/whatever shouldn't matter, just create the string later. If you ",".join(somelist) you get the string 'String1, String2' so you can extend that:

distList = []
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if == "Disturbance":
        for row in arcpy.SearchCursor(lyr):
            if "ds" in row.Field1:
        lyr.definitionQuery = '"Field1" in ({})'.format(", ".join(distList))
share|improve this answer
Thanks Jason. This worked perfectly. For added value, I threw in the 'set' function to eliminate duplicates which occur in my case: lyr.definitionQuery = '"Field1" in ({})'.format(", ".join(set(distList))) – Mike Aug 22 '12 at 21:35
Oh, I just discovered after running my script a few times, that this approach almost solves my problem, but one last issue is, it wraps each items in the list in double quotes instead of single quotes. (i.e. "SMU" in ("O1", "S2", "O20", "O23a") whereas the definition query on likes the items to be wrapped in single quotes ("SMU" in ('O1', 'S2', 'O20', 'O23a'). I need to find a way to replace those double quotes. – Mike Aug 23 '12 at 15:32
Oh, I think you have to just reverse the quotes when formatting (i.e. distList.append("'{}'".format(row.Field1)), double quotes on the outside of {} and single quotes on the inside) – Mike Aug 23 '12 at 15:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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