Hello guys,
When a user selects “Option 1” and “Option 2” in the selectbox, the resulting selection is not returned as the expected string “Option 2” but instead is returned as a list of characters “[O,p,t,i,o,n, ,2]”. Is there a solution to this issue?
Thank you
options = ["Option 1", "Option 2", "Option 3", "Option 4"]
selected_options = st.multiselect("Select options", options)
if len(selected_options)>1:
second_selected_option = selected_options[1]
st.write("The second selected option is:", second_selected_option)
option_no_2 = st.selectbox('The second selected option is:',second_selected_option)
else:
st.warning("You have not selected at least 2 options.")
What are you expecting with the selectbox after the multiselect?
It reads to me that upon making a second selection, you pass the second selection (which is a string) into the options parameter of selectbox. This parameter expects an iterable, so it will indeed parse the string like a list of characters. If you mean to pass it a list of size one, you need to put your one element into a list, but I don’t understand what you would be doing with the selectbox then.
option_no_2 = st.selectbox('The second selected option is:',[second_selected_option])
Ahh okay mathcatsand, thank you for pointing me about list in selectbox, now i change it from second_selected_option to [second_selected_option] and the result is as expected
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.