Multiselectbox with lists as options

Hello there,

i’m sorry if the following is a trivial task but i’ve been trying to wrap my head around it and just couldn’t find a solution by myself.

I want to create a multiselectbox where a user can choose between multiple lists instead of strings.

A quick example what i’m trying to achieve:

list_1 = ['1','2','3','4']
list_2 =  ['a','b','c','d']
list_3 =  ['!','?','€','%']

choice = st.multiselect('Choose List',[list_1,list_2,list_3])
df = df[df['symbol'].isin(choice )]

While the example above sort of works it will logically display the list inside the selectbox.
But i would like to select a string so “choice” is equal to the selected list.
I would also need to join the lists if multiple options are selected.

I need a little hint here what would be the best way to do this.
Thanks in advance for anyone taking his/her time for me.

It is unclear what you want. Do you want the options to be displayed as strings in the widget? That is already happening. Do you want multiselect to return a string? That cannot happen, it always return a list. Do you want to return a list of strings? Then the options must be strings.

There is no selected list. This is a multiselect so there will be a list of selected options. Since the options themselves are lists, that will be a list of selected lists.

That’s easy: sum(choice, []).

Sorry if i didn’t describe it clearly enough.

I want the widget to display the string name (list_1, list_2 and list_3)
and when one options is selected it returns the selected list.

In my example above the widget would display the options like


Since i didn’t input string values but my lists.
Which then would return a list within a list i guess but that’s not what i want.

So for example if i select “list_1”
choice = ['1','2','3','4']
And if i would select “list_1” and “list_3”
choice = ['1','2','3','4','!','?','€','%']

So you want the widget to display this in he dropdown?


Looks easy enough:

names = st.multiselect("Choose List", options=["list_1", "list_2", "list_3"])

As I already said, there is no way a multiselect can do that. You can write code, though. There is life beyond the widgets.

For that you need a mapping between the names and the lists. That is what python dictionaries are for:

lists_by_name = {
    "list_1": ["1", "2", "3", "4"],
    "list_2": ["a", "b", "c", "d"],
    "list_3": ["!", "?", "€", "%"],

Now you can convert the list of selected names in a list of selected lists:

lists = [lists_by_name[name] for name in names]

And finally merge the selected lists as I explained in my previous post:

choice = sum(lists, [])

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.