3

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 lyr.name == "Disturbance":
           for row in arcpy.SearchCursor(lyr):
                if "ds" in row.Field1:
                     distList.append(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

1 Answer 1

5

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 lyr.name == "Disturbance":
        for row in arcpy.SearchCursor(lyr):
            if "ds" in row.Field1:
                distList.append('"{}"'.format(row.Field1))
        lyr.definitionQuery = '"Field1" in ({})'.format(", ".join(distList))
3
  • 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
    Commented Aug 22, 2012 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
    Commented Aug 23, 2012 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
    Commented Aug 23, 2012 at 15:54

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.