I'm trying to create a distinct list of values in my Python Addin combo box drop down list

1605
3
Jump to solution
07-13-2016 01:38 AM
OLANIYANOLAKUNLE
Occasional Contributor II

I'm trying to create a distinct list of values in my Python addin combo box drop down list; i already have the script that actually shows all the values of the field appended to my combo box drop down. What i intend to achieve with this is to have a list of distinct values on my drop down - see my code below;

def __init__(self):
        OBJECTID = []
        for row in arcpy.SearchCursor("BldForm"):
            OBJECTID.append(row.Combo)
        self.items = sorted(OBJECTID)
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'
   
    def onSelChange(self, selection):
        
        mxd = arcpy.mapping.MapDocument("Current")

        for df in arcpy.mapping.ListDataFrames(mxd):
            if df.name == "Layers":
                dfmain = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]


        OBJECTID = []
        for row in arcpy.SearchCursor("BldForm"):
            OBJECTID.append(row.Combo)

I'll kindly appreciate your help on this.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Two suggestions:

  1. use an arcpy.da.SearchCursor, they are much quicker than the old arcpy.SearchCursor
  2. use a set to get unique values

        with arcpy.da.SearchCursor("BldForm", ['Combo']) as rows:
            OBJECTID = sorted(set([row[0] for row in rows]))

View solution in original post

3 Replies
Luke_Pinner
MVP Regular Contributor

Two suggestions:

  1. use an arcpy.da.SearchCursor, they are much quicker than the old arcpy.SearchCursor
  2. use a set to get unique values

        with arcpy.da.SearchCursor("BldForm", ['Combo']) as rows:
            OBJECTID = sorted(set([row[0] for row in rows]))
RebeccaStrauch__GISP
MVP Esteemed Contributor

Another option and many other nice snippets Some Python Snippets

OLANIYANOLAKUNLE
Occasional Contributor II

Thanks for saving my day!!!!

0 Kudos