Setting the entries in a value list?

This version of @AndersDeleuran’s PVL (PopulateValueList) Python component has changes to fix a few anomalies I encountered:

  1. Added code to accept only Keys. If no Values are are connected they will default to the Keys.
if not Values:
    Values = Keys
  1. Added Type hint = ‘str’ (String) to all three inputs, making it possible to use integers without error.

  2. Handle empty Value Lists by changing this:
    selected = obj.FirstSelectedItem.Name
    to this:
    selected = obj.FirstSelectedItem

"""
Populate Value List
    Inputs:
        Name: {item,str}
        Keys: {list,str}
        Values: {list,str}
    Outputs:
    Remarks:
        Author: Anders Holden Deleuran (BIG IDEAS)
        Rhino: 6.30.20266.14531
        Version: 210108
        misc. debugs by Joseph Oster, Jan 4, 2024
"""

ghenv.Component.Name = "PopulateValueList"
ghenv.Component.NickName = "PVL"

import Grasshopper as gh

if not Values:
    Values = Keys
if Keys and Values:
    for obj in ghenv.Component.OnPingDocument().Objects:
        if type(obj) is gh.Kernel.Special.GH_ValueList:
            if obj.NickName == Name:
                selected = obj.FirstSelectedItem
                obj.ListItems.Clear()
                for k,v in zip(Keys,Values):
                    vli = gh.Kernel.Special.GH_ValueListItem(str(k),str('"' + v + '"'))
                    obj.ListItems.Add(vli)
                if selected in Keys:
                    obj.SelectItem(Keys.index(selected))
                obj.ExpireSolution(True)

PVL_2024Jan4a
PVL_2024Jan4a.gh (9.1 KB)

Please contact me if there are further Python refinements to be made.