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
-
On a related note, I found this, which is sort of what I want, but by deleting. gis.stackexchange.com/questions/236779/…william3031– william30312019年08月22日 04:58:16 +00:00Commented Aug 22, 2019 at 4:58
1 Answer 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
Explore related questions
See similar questions with these tags.