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

TomazicM
27.3k25 gold badges33 silver badges43 bronze badges
asked Aug 22, 2019 at 4:20
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
answered Aug 22, 2019 at 4:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.