Create for loops in a drop down when value and label are different

I have a dataframe with two columns, one is a string and the other is a number. I want my dash dropdown menu to have a ‘label’ field for each unique entry of the string column, and the ‘value’ field of the dropdown to be the column which contains the number on the same row. How would I do this?

I thought it might be something like this, but it doesn’t work, because it functions as a for loop of a for loop.

options=[ {'label': i, 'value': j} for i in court_code_df['court_name'].unique() for j in court_code_df['mis_data_source_code'].unique() ],

Example data:

court_name                  mis_data_source_code
aaa                                       562
bbb                                       435

So I can see the options of ‘aaa’, ‘bbb’ in the dropdown, but the value returned by ‘aaa’ would be 562, and for ‘bbb’ it would be 435, and so on for all the columns in the ‘court_code_df’ dataframe.

I’ve not tried to test your code but I think what you are looking for is zip, which will iterate through 2 iterables at the same times e.g.:

[{'label': i, 'value': j} for i, j in zip(df['foo'].unique(), df['bar'].unique())]

There’s probably also some nice DataFrame specific tricks to get the same result more efficiently.

Thanks that worked :slight_smile: