Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a table:

enter image description here ,

Script at ModelBuilder's "calculate values":

Expression:

a("%path%")

Code BLOCK:

def a(path):
  fields = arcpy.ListFields(path,"FID_*")
  for field in fields:
    return field.name

Gives me only ONE field name!

share|improve this question

2 Answers 2

up vote 8 down vote accepted

Yes. The return statement short-circuits it. Basic Python.

def a(path):
  field_names = []
  fields = arcpy.ListFields(path,"FID_*")
  for field in fields:
    field_names.append(field.name)
  return field_names

Though you may need to do it this way:

def a(path):
  field_names = []
  fields = arcpy.ListFields(path,"FID_*")
  for field in fields:
    field_names.append(field.name)
  return ";".join(field_names)
share|improve this answer
2  
Just to reinforce that, when Python encounters a return statement, it will always exit the function, regardless of a loop. –  sgrieve Sep 27 '12 at 7:38
    
thx, this f. sh. makes me sad T_T –  Vasiya Sep 27 '12 at 8:08
    
I use arcpy.ListFields(fc,"*","All") where fc is the target feature class. –  Timothy Michael Sep 27 '12 at 19:30
def a(path):
  fields = arcpy.ListFields(path,"FID_*")
  return fields
share|improve this answer
    
Someone else had downvoted this so I have countered that, but in any event what will be returned by your code is a list of field objects rather than a list of field names. It is well worth trying this code and then the code in the other Answer to see the difference. –  PolyGeo Nov 4 '13 at 5:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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