Get the previous selection of a slider

I am currently developing an important streamlit app, but I am facing one issue, which I wasn’t able to solve yet.

I have a range slider for years. The problem now is, that I want to have the previous selection of this slider. After clicking a specific button, the previous year range should set itself to the current year range (because after that the previous year range should be considered as the current year range). I will explain the exact problem below the code, because you can understand it better after reading the code.

Here is what I currently have:

   # At the start of the app
    if 'year_range' not in st.session_state:
        st.session_state.year_range = [
            2000, 2022]
    if 'previous_year_range' not in st.session_state:
        st.session_state.previous_year_range = [
            2000, 2022]```
   # The slider where the user can select the year range (st.session_state.min_max is not important for this problem, so you can ignore it)
   st.session_state.year_range[0], st.session_state.year_range[1] = 
        "Select years range:",  min_value=st.session_state.min_max[0], value=st.session_state.year_range, max_value=st.session_state.min_max[1])

    # The button -> When clicked, set previous_year_range to 
    button = col1.button('Submit and Compare')
    if button:

def populate_graph():
   if st.session_state.year_range != st.session_state.previous_year_range
      clear_graphs() # Function that should run when the year_range changed
      st.session_state.previous_year_range = st.session_state.year_range # Reset the previous year range to the new year range

So, what is the problem now exactly?

If I change the year range the first time, I can see, that the year range has changed and previous year range is not equal to year range.

Then, after clicking the button, populate_graph is executed and because they are unequal, it executes the if statement. Now I change the year slider again but now, previous year range sets itself automatically to year range, even if the button wasn’t clicked. I assume, it has something to do with st.session_state.previous_year_range = st.session_state.year_range, but I can’t figure out why it sets itself to the year range.

It would be very great if you could help me, I have a deadline till tomorrow and have to fix this bug till then :see_no_evil: