Home > Net >  Can you loop Dash dropdown entries?
Can you loop Dash dropdown entries?

Time:02-01

I am trying to create a dropdown list with around 600 entries. Is there a way to write out the entries with out writing every line out. Like this:

dcc.Dropdown(
            id='select-stat-dropdown',
            options=[{'label': labels[0], 'value': values[0]},
                     {'label': labels[1], 'value': values[1]},
                     {'label': labels[2], 'value': values[2]},
                     {'label': labels[3], 'value': values[3]},
                     {'label': labels[4], 'value': values[4]},
                     {'label': labels[5], 'value': values[5]},
                     {'label': labels[6], 'value': values[6]},
...

I have a list for every label and value:

values = list(playersDF)
label = []

Is there a way to implement a loop to write them out for me?

CodePudding user response:

We can use a list comprehension to create the variable options

options = [{'label': labels[i], 'value':values[i]} for i in range(len(labels))]

and then pass this to the options parameter:

dcc.Dropdown(
    id='select-stat-dropdown',
    options=options
    ...
)
  •  Tags:  
  • Related