# Renaming several fields using arcpy.AlterField_management()?

After several attempts and researches (I haven't found examples for renaming several fields directly without using a list), I tried the following code to rename several fields. However, I get the following:

ERROR 000664: Invalid input: The type of dataset is not supported.

arcpy.AlterField_management(in_table = "file.shp",
field = ['Old_title','Old_title_1','Old_title_2'],
new_field_name = ['New_title','New_title_1','New_title_2'])

• Even if AlterField worked on shapefiles, the field names you've chosen are invalid -- the dBase III+ name length limit is 10 characters. – Vince Aug 3 '18 at 11:02

## 2 Answers

Firstly, as mentioned by @orbl_soil, you should be using a feature class (or table) from a geodatabase to use AlterField_management. Secondly, you should make sure that your feature class (or table) is fully qualified, including the full path of the workspace and feature class (or table). Eg, fc = r"C:\path\to\workspace\feature_class"

Then it's a simple one liner using Python list comprehension to modify multiple field names. However, as @Paul alluded to below, this doesn't really offer any benefits over a more explicit/two-line for loop apart from the fact that it is all in one line. It also has the minor drawback of producing an unused list result (in this case, something like [None, None, None, None]) which is a small waste of resources.

But if you would like to pursue this method, here are a few examples, which demonstrate how it can be done for different situations:

Example 1 - Use a dictionary of old/new field names for each field you want to alter:

namesDict = {"a": "a_new", "b": "b_new", "c": "c_new"}

[arcpy.AlterField_management(fc, f, namesDict[f]) for f in namesDict]


Example 2 - Append "_renamed" to all non-required field names:

[arcpy.AlterField_management(fc, f.name, f.name+"_renamed") for f in arcpy.ListFields(fc) if not f.required]


NB: In the example above, it changes all field names that can be changed. Required fields, such as OBJECTID and Shape_Length, cannot have their names altered.

Example 3 - After performing a 'dissolve' or 'summary statistics' the resulting feature class or table will usually have several fields with "FIRST_" or "SUM_" or similar prefixed to them. Sometimes you don't want this, so you can easily remove these prefixes by:

[arcpy.AlterField_management(fc, f.name, f.name.replace("FIRST_", "")) for f in arcpy.ListFields(fc, "FIRST_*")]


Equivalent for Loop Examples - Using a two-line loop, that does not waste resources creating an unused list, and is more 'pythonic' in the sense that clarity is more important than conciseness, the equivalents of the above three examples would be:

Eg 1:

for fieldName in namesDict:
arcpy.AlterField_management(fc, fieldName, namesDict[fieldName])


Eg 2:

for fieldName in [f.name for f in arcpy.ListFields(fc) if not f.required]:
arcpy.AlterField_management(fc, fieldName, fieldName+"_renamed")


Eg 3:

for fieldName in [f.name for f in arcpy.ListFields(fc, "FIRST_*")]:
arcpy.AlterField_management(fc, fieldName, fieldName.replace("FIRST_", ""))

• I really like how functional the dictionary solution is. Works like a charm. Thanks – ePoQ Aug 3 '18 at 7:35
• You can simplify if f.name not in ("OBJECTID", "SHAPE", "SHAPE_Area", "SHAPE_Length") to if not f.required. – Tom Aug 6 '18 at 15:12
• @Tom - good point. I've updated the post with your suggestion. – Son of a Beach Aug 6 '18 at 22:35
• I'm all for one-liners, but just use a for loop here. Reserve your comprehensions when you actually need the list afterwards. – Paul Aug 7 '18 at 0:42
• Thanks @Paul. I've added a caveat to the post that I hope expresses your concerns adequately. – Son of a Beach Aug 7 '18 at 0:58

The documentation says the tool only works on geodatabases, not shapefiles. You'll have to import your data into a local file geodatabase before running your code.