Session State, Update data from list in list

Hi there, i have this code like this

import streamlit as st

#form di sidebar
with st.sidebar: 
    st.title("Isikan Data")
    inputvalue = st.text_input("Write Something", value="Item 1")

if 'data' not in st.session_state:
    st.session_state['data'] = []

number_of_elements = len(st.session_state['data'])

if button:
        st.error('Error', icon="🚨")

#Delete sesssion state  
with st.sidebar:
    nomersesi= len(st.session_state['data'])
    listnumber = list(range(1,addone))
    delete_selectbox=st.selectbox("Delete Item", (listnumber))
    deletebutton = st.button("Delete Item")
if deletebutton:
    del st.session_state['data'][delete]
    st.session_state['data'][0][0]= listnumber[0]#THIS CODE NEED REVIEW


If i press “Submit” Button, i can add information that i write before in st.text input. I press “submit” button multiple times, and i get my information.

When i press “Delete” Button. i can delete that i want to delete.

For example that is my result :
[[1,“Item 1”,“information”],[2,"Item 2,“information”],[3,“Item 3”,“information”],[4, “Item 4”, “information” ]]

For example i want to delete [2,"Item 2,“information”]

And then i want to get the result like this :
[[1,“Item 1”,“information”],[2,“Item 3”,“information”],[3, “Item 4”, “information” ]]

So, i want to update my number by this code

st.session_state['data'][1][0]= 2
st.session_state['data'][2][0]= 3

i think that code not efficient, i want to make more efficient, please give me advice, how to update number in list , with more efficient code

Hi @Arief_Surya

One approach could be to put the data into a Pandas DataFrame, then delete the desired row, afterwards renumber the index and finally retrieve the renumbered data from the DataFrame and update the session state variables.

I made an example code here at Solution to: Session State, Update data from list in list · GitHub

Another approach may be to use a for loop to renumber the first value in your lists by subtracting the first value by 1 (for rows that are found after the deleted row).

Hope this helps!

Best regards,

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