0

I need to get a list of field names that only includes editable fields and not the geometry field, but then add the geometry field back to the end of the list as SHAPE@ token. I have code that runs successful, but I am horrible with List Comprehensions and I am sure there is something better out there.

update_fieldsED = [f for f in fields if f.editable]
update_fieldsNoShape = [fNS.name for fNS in update_fieldsED if fNS.type not in ["Geometry"]]
update_fieldsNoShape.append("SHAPE@")

Is there a more compact way to achieve this?

  • 1
    What is wrong with what you're doing? There is no inefficiency. Are you having trouble with it, or is something not working? List Comprehension isn't going to make your code quicker or better, it just makes it more compact. I believe this comes at the expense of some readability particularly for someone fair new to python. – Midavalo Jul 14 '17 at 19:52
  • Would changing 'better' to 'more compact' make this less "opinion-based"? I dont know how that wouldnt be a valid question. – Rex Jul 18 '17 at 19:51
  • possibly, although I still also believe you should include some info on why you're trying to do it, what the end goal is, what has caused you to look for this answer. Are you having trouble or hit a problem you need to have solved? – Midavalo Jul 18 '17 at 20:26
5

Here's an easy way:

fields = arcpy.ListFields('//path/to/layer')
flds = [f.name for f in fields if f.editable and f.type != "Geometry"] + ["SHAPE@"]
| improve this answer | |

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