Sign up ×
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'm trying to add multiple fields to a table using arcpy and am wondering if it's possible to have multiple fields declared in the AddField management, and their values, separated by commas or something? Here is the code as stands, which is slightly cumbersome:

import arcpy, sys

arcpy.env.workspace = "C:\\gislab2\\Python\\take_home"
shapefile = "USCancer2000.shp"

#Set fieldname variables
field_name1 = "Crdrt"
field_name2 = "Crdrt_min"
field_name3 = "Crdrt_max"
field_name4 = "Ageadj"
field_name5 = "MinAgeAdj"
field_name6 = "MaxAgeAdj"

###Declare each standard population per age group, and for summed standard population

    #Add the fields one at a time 
    arcpy.AddField_management(shapefile,field_name2,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name3,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name4,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name5,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name6,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")

I know there's a way to create a loop to do it, but was wondering if there was a simpler and more direct way.

share|improve this question
This sounds like a homework question, but I would say that adding your fields in a loop is the most efficient way to go assuming your field types are all the same. –  RyanDalton Apr 7 '13 at 19:00
self-edification, not homework. –  David Meek Apr 7 '13 at 22:42
The file paths from -all- of your posts suggest otherwise. –  nagytech Apr 7 '13 at 23:31
just because I'm in a class doesn't equate a question with homework. –  David Meek Apr 8 '13 at 0:02

4 Answers 4

A few techniques involved here. First off, I am storing all the arguments as tuples in a list. I can iterate through the list using a for-in loop. The tuples can be unpacked into function arguments using the * operator. I could have just gone with field names in a list, but then you would have not been able to be flexible in the other attributes of the field.

The shapefile, though, does stay constant throughout (and I assume that the name of the shapefile is stored in the variable shapefile as above). So, I do tuple concatenation inside the loop to build my entire set of arguments:

(shapefile,) + field

The trailing comma on shapefile, is necessary to tell the interpreter that you are creating a tuple with only one element. The end result is a tuple that can be unpacked with the * operator. As an example, the first tuple processed will be:


Notice that tuple concatenation takes precedence over the * operator, so that the entire concatenated tuple is unpacked instead of just (shapefile,).

fields = [
for field in fields:
    arcpy.AddField_management(*(shapefile,) + field)
share|improve this answer
A really useful answer, was trying to achieve this with dictionary first, but then recalled the tuples and saw your answer :) –  Alex Tereshenkov Sep 10 '13 at 15:12

You may want to consider not using Python at all on this one but the advice here can also be used within a Python script.

If you want to avoid multiple AddFields consider creating a single empty table with all those fields and then using Join Fields based on the OID field.

It should join them all in one go.

You could create your empty Table manually by creating your new Table in the Catalog window or use Create Table/Add Field(s) in a Python script.

share|improve this answer

I like Brett's original answer but as 99% of the time I only modify Field Name, Type and Length - use the following (can be adapted to expose more parameters if required).

# Define the field parameters
fields = [

# Create the fields using the above parameters
for field in fields:
    arcpy.AddField_management(secc_maps_point, field[0], field[1], "", "",
                              field[2], "", "NON_NULLABLE", "NON_REQUIRED", "")
share|improve this answer

I usually use one-liner list comprehension for such cases:

[arcpy.AddField_management(shapefile,field_name,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","") for field_name in ['Crdrt', 'Crdrt_min', 'Crdrt_max', 'Ageadj', 'MinAgeAdj', 'MaxAgeAdj']]
share|improve this answer

Your Answer


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.