6

I'm using arcpy.ListFields() to get a list of fields in my Feature Class. Based on my limited testing of several geodatabases and tables, the order of fields returned is the field order specified in the database.

Can I depend on ListFields() to return fields in the same order and therefore use a field's position in that list to consistently get the same field? For example, if the third field is "Name", can I use fields[2] to reliably obtain the "Name" field? Or, is it just coincidence that it's been working thus far?

The help page does not address this question.

  • +1 for the general question (I'd also be interested to know). For your specific situation, you'd also need to be confident that your input table(s) will always be so ordered, but that's perhaps outside the scope of what you asked. – Erica Feb 11 '15 at 18:14
  • @Erica, I'm trying to avoid a dependency on column order, even in my input and output tables. I'd like to query the tables for fieldnames and use their current positions and then map fields based on name. For example, If "Name" is in fields[2] in the input table but fields[9] in the output table, my code would look in a mapping table to match "Name" to "Name". I only want to map field names and not care about position. This whole exercise is about mapping fields of differing names. "Name" to "Site_Name" is probably more realistic. – DenaliHardtail Feb 11 '15 at 18:22
  • You can always just check the field name property. If not too cumbersome, set up a dictionary with source field as the key, destination key as the value, then iterate through the dictionary. There's also the field mapping object, but I've never used that, so not qualified to give advice there. – recurvata Feb 11 '15 at 20:12
2

Yes, ListFields will return fields in the same order. In this example, 10 iterations of ListFields are performed. In the sample feature class, I added fields A - H in order.

import arcpy, os

fc = r'C:\test\myGDB.gdb\test'

for item in range(1,10):
    fields = [f.name for f in arcpy.ListFields(fc)]
    print fields

enter image description here

  • That's all in one session. I wouldn't count on it being the same across sessions (but wouldn't be surprised if it was). Longer term, I'm always hesitant to rely on any index or retrieval system which uses position number instead of name or ID. – matt wilkie Feb 19 '15 at 16:44

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.