0

I have seen this answer to Calling specific columns of attribute table using ArcPy?, but it uses a slice to select columns.

I would like to select them using a list.

Example code that doesn't work:

import arcpy    
select_cols = ['FID', 'Shape', 'OBJECTID'] # the list of columns
    for field in field_list[select_cols]:
        print(field.name)

Traceback (most recent call last): File "", line 1, in TypeError: list indices must be integers or slices, not list

A different attempt

field_list2 = field_list[select_cols]

Traceback (most recent call last): File "", line 1, in TypeError: list indices must be integers or slices, not list

1

Assuming you need the field objects (and not just the field names)...

You could use a list comprehension. For example:

for field in [f for f in field_list if f.name in select_cols]:
    print field.name

Or without a list comprehension, it could be done like:

for field in field_list:
    if field.name not in select_cols:
        continue
    print field.name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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