When executing the add field command in arcpy it always appends the field at the end of an attribute table.

Can someone help me write a code where a field can be inserted in the middle of an attribute table by parsing the data in the fields that would come after the new row to a dictionary, then deleting those rows, adding the new field and then recreating the old fields that were deleted and parsing the data back into the fields?

I know it seems like a lot of extra work but there are times when this would be helpful.

  • 2
    If you really want to do this, the easiest way would be to create a new featureclass, adding all existing fields and the new field in the desired order to it, and then copy all the data from the old featureclass to the new one. But maybe you could edit your question and explain when exactly the order matters. This sounds to me like an XY problem – Berend Dec 4 '18 at 15:53
  • That would work for some instances, in some cases however I have global ID's enabled on the data so creating a new feature class wouldn't allow for the retention of the global ID's – gisanalyst Dec 4 '18 at 15:57
  • 2
    Answer with code here: gis.stackexchange.com/questions/32119/… – GBG Dec 4 '18 at 16:05
  • 4
    just curious why it matters that the field be in the middle. you can move the fields around in attribute table when you work with them (same goes for if you are using them in web maps/apps.) This link community.esri.com/blogs/richard_fairhurst/2014/11/08/… will show you step by step how to create an updatecursor to do what you are wanting to do. – ed.hank Dec 4 '18 at 16:06