Dependency Between Selectbox and a Changing List

Dear Streamlit Team,

Thank you for your amazing product. I am trying to use the selectbox widget to remove items from a list. However, this only works—with the code below—if I remove the items in order, i.e., first “a”, then “b”, then “c”. If I, for example, want to remove “b” before “a” the app freezes and the console gives me the exception below.

Minimal Code Sample:

import streamlit as st

if st.button("Fill") or "options" not in st.session_state:
    st.session_state.options = ["a", "b", "c"]


def submit():
    st.session_state.options.remove(st.session_state.selected)


form = st.form("My form")
form.selectbox("Select", st.session_state.options, key="selected")
form.form_submit_button("Submit", on_click=submit)

Error Message

Exception in thread ScriptRunner.scriptThread:
Traceback (most recent call last):
  File "/Users/arc/opt/anaconda3/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/arc/opt/anaconda3/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/arc/opt/anaconda3/lib/python3.8/site-packages/streamlit/script_runner.py", line 182, in _process_request_queue
    widget_states = self._session_state.as_widget_states()
  File "/Users/arc/opt/anaconda3/lib/python3.8/site-packages/streamlit/state/session_state.py", line 447, in as_widget_states
    return self._new_widget_state.as_widget_states()
  File "/Users/arc/opt/anaconda3/lib/python3.8/site-packages/streamlit/state/session_state.py", line 198, in as_widget_states
    states = [
  File "/Users/arc/opt/anaconda3/lib/python3.8/site-packages/streamlit/state/session_state.py", line 201, in <listcomp>
    if self.get_serialized(widget_id)
  File "/Users/arc/opt/anaconda3/lib/python3.8/site-packages/streamlit/state/session_state.py", line 179, in get_serialized
    serialized = metadata.serializer(item.value)
  File "/Users/arc/opt/anaconda3/lib/python3.8/site-packages/streamlit/elements/selectbox.py", line 113, in serialize_select_box
    return index_(opt, v)
  File "/Users/arc/opt/anaconda3/lib/python3.8/site-packages/streamlit/util.py", line 129, in index_
    raise ValueError("{} is not in iterable".format(str(x)))
ValueError: b is not in iterable
1 Like

Hi @arc,

Similarly to my other answer, can you please try without the state element? Does it get rid of the issue?

Thanks
Charly