0

As part of a larger project i'm looking to add multiple fields to a shapefile using an index. This is the snippet in question:

listFields = [['ITEM', 'TEXT'], ['SYSTEM', 'TEXT'], ['WATERCOURCE_BANK', 'TEXT'], ['AIMS_REF', 'TEXT'],
          ['CHAINAGE', 'TEXT'], ['GRID_REF', 'TEXT'], ['CUT', 'TEXT'], ['LENGTH_KM', 'TEXT'], ['COMMENTS', 'TEXT']]

print("Adding fields")
for addField in listFields:
print("   ...{}".format(addField[0]))

arcpy.AddField_management('table_stamp', addField[0], addField[1], addField[2], addField[3], addField[4],
                          addField[5], addField[6], addField[7], addField[8])

I'm getting the following error, pointing at the line of code above:

IndexError: list index out of range

As far as I can tell I have got my Indexing right. Nine fields to add so the indexes should be 0 to 8.

Anybody have any idea what the issue might be?

1
  • Each iteration of addField only has 2 elements. The references to addfield[2] and above are what are causing the IndexError.
    – Bjorn
    Commented Nov 28, 2018 at 21:01

1 Answer 1

4

Add field only adds one field at a time. You will need to add an AddField for each field. Something like...

arcpy.AddField_management('table_stamp', addField[0][0], addField[0][1])
arcpy.AddField_management('table_stamp', addField[1][0], addField[1][1])

or

for item in listffields:
    arcpy.AddField_management('table_stamp', item[0], item[1])
1
  • I went for the second option and that seemed to do it, thank you. Commented Nov 29, 2018 at 8:24

Your Answer

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

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