# Choosing string from dropdown list rather than typing it into Python script tool?

As you can see, in the above picture, I need to input the county name to finish the parameter setup. Can I change the last part from inputting string to choosing an item from a dropdown list? For example, there are five potential counties I will run the model, like A, B, C, D and E. I want to choose one of them from a drop-down list rather than type the input. I think the Data Type should be something else rather than the String, as shown in the below picture. Any suggestions?

I am using ArcGIS 10.3.1 Advance License.

There are a couple of ways to approach this:

1. Create a list of filter values in the parameters property.
2. Create the list in the validation script.

I prefer the second route since you have more control over what values are valid as well as extracting unique values from fields.

Going the first route:

Set the data type to string (as shown in your picture) and change the filter type from None to Value List. Here you can add in all the values you want to see in a drop down list. Going the second route:

The advanced route requires modifying your script validation. While this may be overkill based on the your needs, I thought it might be useful in giving you an idea of a more efficient approach that may help for future scripts.

You will need to first initialize the variable under the initializeParameters(self) function. The parameter in your script that will need a dropdown list is based on the parameter number assignment. In your example, you want the 7th parameter, so you will assign values to self.params[6] = "List of Values" (Remember that python index's starts 0)

In the example below, I'm creating a county list for the first parameter self.params[0]. I create an interim dictionary that stores all the municipalities for each county so the dictionary key/value structure = county:[municipalities]. Whatever county the user selects will be passed into the muninicipality dictionary. The drop down list for the second parameter self.params[2], is a list of the municipalities in whatever county. You will also need to add in logic check under the updateParameters(self) function to account for when a user changes the value in the first parameter.

import arcpy
global county_mun_dict

class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""

def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()

def initializeParameters(self):
"""Refine the properties of a tool's parameters.  This method is
called when the tool is opened."""
self.params[0].filter.list = Get_County_List()
municipality_list = Get_Municipality_List("ATLANTIC")
municipality_list = sorted(municipality_list.keys())
self.params[1].filter.list = municipality_list

#Setup wildcard dropdown list
query_type = ["Equals", "Contains"]
self.params[4].filter.list = query_type

return

def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed.  This method is called whenever a parameter
has been changed."""
if self.params[0].altered:
municipality_list = Get_Municipality_List(self.params[0].value)
municipality_list = sorted(municipality_list.keys())
self.params[1].filter.list = municipality_list
return

def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter.  This method is called after internal validation."""
return


Resources:

ArcGIS Resources:

• Can you replace your "picture of code" with the code as text, please? There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. Otherwise its a great answer! – PolyGeo Nov 14 '16 at 21:24
• @PolyGeo, I was having trouble getting it properly formatted so I just tabbed each line. How do you properly use the {} format function? I checked out the this link for but didn't see anything with the {} code formatting. – Sethdd Nov 14 '16 at 21:34
• That link is to comment formatting (which is very limited because we do not want comments to be used in place of Q&As). In questions and answers, simply highlight the text and click the format button. It will insert those four spaces on all highlighted lines, and do syntax highlighting for you. – PolyGeo Nov 14 '16 at 21:41

Thanks for the demonstration.

Just wanted to offer my experience:

For option 2 above the Get_Municipality_List and Get_County_List functions can be defined within the validator script file (outside and prior to the ToolValidator class definition worked for me).

You still need to set the parameter filter value to 'value list' as shown in option 1 above.

To get a list of check boxes (multiselect) set the MultiValue parameter property to Yes. in my case i'm using census data so my function is called GetDemographicsList instead:

    def GetDemographicsList():
dl = []
dl.append("Total Population")
dl.append("Population by Age")
dl.append("Male Pop by Age")
dl.append("Female Pop by Age")
dl.append("Median Age")

return dl
import arcpy
class ToolValidator(object):
...