Working with multivalue inputs

Many geoprocessing tools have input parameters that accept multiple values. When you view the tool's reference page or its usage in the Python window, whenever you see the parameter enclosed in brackets [ ], you know it can take a list of values. For example, the Delete Field tool takes a list of fields to delete and the parameter usage is displayed as [drop_field, ...]. Some parameters, such as the input_features parameter of the Union tool, take a list-of-lists; its usage is displayed as [[in_features, {Rank}], ...].

Any parameter that accepts a list of values (or a list of list of values) is a multivalue parameter—it accepts one or more values. There are three ways to specify a multivalue parameter:

  1. As a Python list, where each value is an element in the list
  2. As a string, where values are separated by semicolons
  3. As a ValueTable, where values are stored in a virtual table of rows and columns

Below are examples of each.

As a Python list

In a script, multivalue inputs can be passed as a Python list. A list is enclosed in brackets and is a flexible Python type.

DeleteFields using a Python list for the drop_field parameter

import arcpy 
from arcpy import env 
env.workspace = "C:/base/county.gdb"
arcpy.DeleteField_management("roads", ["STREET_NAM", "LABEL", "CLASS"])

Union using a Python list for the in_features parameter

import arcpy 
from arcpy import env 
env.workspace = "C:/base/data/gdb" 
arcpy.Union_analysis([["counties", 2],["parcels", 1]], "state_landinfo")

As a string

Your script may have to use a multivalue string in some cases, because one may be returned as an output value of a tool or passed as an input parameter for your script.

DeleteFields using a multivalue string for the drop_field parameter.

import arcpy 
from arcpy import env 
env.workspace = "C:/base/county.gdb"
arcpy.DeleteField_management("roads", "STREET_NAM;LABEL;CLASS")

Union using a multivalue string for the in_features parameter

import arcpy 
from arcpy import env 
env.workspace = "C:/base/data/gdb" 
arcpy.Union_analysis("counties 2;parcels 1", "state_landinfo")

With ValueTable

A ValueTable allows you to organize values into a virtual table of rows and columns. You specify the number of columns when you create a value table. The default is a single column.

DeleteFields using a ValueTable for the drop_field parameter

import arcpy 
from arcpy import env 
env.workspace = "C:/base/county.gdb"

vt = arcpy.ValueTable()
vt.addRow("STREET_NAM")
vt.addRow("LABEL")
vt.addRow("CLASS")

arcpy.DeleteField_management("roads", vt)

Union using a ValueTable for the in_features parameter

import arcpy 
from arcpy import env 
env.workspace = "C:/base/data/gdb" 

vt = arcpy.ValueTable(2)
vt.addRow("counties 2")
vt.addRow("parcels 1")

arcpy.Union_analysis(vt, "state_landinfo")

Related Topics


12/15/2011