Iterate dictionary in streamlit

Hi all. I want to make dictionary with iteration in streamlit closed to something like this:-`

    dict = {}
    num = input("How many elements?")
    for i in range(int(num)):
        k = str(input("Insert your key "))  
        v = float(input("Insert your value" ))  
        dict.update({k:v})
    st.write(dict)

I try to change it to widget st.text_input for the “key” and “value” but it shows error like this:

Is there any other idea I can do this in streamlit?

Thank you

Try adding str(i) as a key to the text input fields or some random strings that change on each iteration.

2 Likes

Hi, I tried it and it worked. Thank you. Just, the widget will replicate to the number of my “num” variable input. Is there any other way that I can just have one input widget (for k and v respectively)?

Try using this kind of syntax:

placeholder = st.empty()
for loop:
    with placeholder.container():
           input_field1
           input_field2
           submit button
           if submit button:
                  continue

Refer to this st.empty - Streamlit Docs

Not entirely sure if this will work. Add conditions like after submit button the loop should progress to the next iteration.