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

1 Answer 1

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
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.