# see the error "KeyError: 'my\_container'"

**URL:** <https://discuss.streamlit.io/t/see-the-error-keyerror-my-container/121877>\
**Category:** Using Streamlit\
**Tags:** session-state, discussion\
**Created:** [July 6, 2026, 6:04pm UTC](https://discuss.streamlit.io/t/see-the-error-keyerror-my-container/121877 "2026-07-06T18:04:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![muhammadfahimaamir](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/muhammadfahimaamir/32/75301_2.png) [@muhammadfahimaamir](https://discuss.streamlit.io/u/muhammadfahimaamir)\
**Post date:** [July 6, 2026, 6:04pm UTC](https://discuss.streamlit.io/t/see-the-error-keyerror-my-container/121877/1 "2026-07-06T18:04:41Z")

</div>

Dear Sir

i use this below code it run but give one error

please see the code and error

> import streamlit as st
> 
> from streamlit\_dnd import dnd, apply\_move
> 
> # 1. Initialize your list in session state
> 
> if “my\_items” not in st.session\_state:
> 
> ```
> st.session_state.my_items = {
> 
> "list": \["Item 1", "Item 2", "Item 3"\]
> 
> }
> 
> ```
> 
> # 2. Render the elements in your Streamlit containers
> 
> with st.container(key=“my\_container”):
> 
> ```
> for item in st.session_state.my_items\["list"\]:
> 
> with st.container(key=f"item\_{item}"):
> 
> st.write(item)
> 
> ```
> 
> # 3. Capture the drag and drop event (must be called AFTER rendering)
> 
> event = dnd(“my\_container”)
> 
> # 4. Apply the move to session state
> 
> if event:
> 
> ```
> apply_move(event, st.session_state.my_items)
> 
> st.rerun()
> 
> ```

this is error

> KeyError: ‘my\_container’
> 
> File “D:\pyff\contt.py”, line 21, in  
> apply\_move(event, st.session\_state.my\_items)  
> ~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
> File “C:\Users\user\AppData\Local\Programs\Python\Python314\Lib\site-packages\streamlit\_dnd\__init_\_.py”, line 267, in apply\_move  
> src = lists[event.from\_container]  
> ~~~~~ ^^^^^^^^^^^^^^^^^^^^^^

 ![image](https://global.discourse-cdn.com/streamlit/original/3X/9/6/96bd185390409c3a61b991cc6e5c14d2dbefd6e1.png)

please see error and give me the solution

best regared

---

<div class="post-metadata">

**Author:** ![AgentStreamy](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/agentstreamy/32/75102_2.png) [@AgentStreamy](https://discuss.streamlit.io/u/AgentStreamy)\
**Post date:** [July 6, 2026, 6:05pm UTC](https://discuss.streamlit.io/t/see-the-error-keyerror-my-container/121877/2 "2026-07-06T18:05:04Z")

</div>

Welcome to the Streamlit community! Thanks for sharing your code and error details. 😊

Your error happens because the key used in your `st.container(key=...)` (“my\_container”) does not match the key in your `st.session_state.my_items` dictionary (“list”). The `apply_move` function expects the container key (“my\_container”) to be present in your items dictionary, but your dictionary uses “list” as the key. To fix this, change your session state initialization to use “my\_container” as the key:

```python
import streamlit as st
from streamlit_dnd import dnd, apply_move

# 1. Initialize your list in session state
if "my_container" not in st.session_state:
    st.session_state.my_container = {
        "list": ["Item 1", "Item 2", "Item 3"]
    }

# 2. Render the elements in your Streamlit containers
with st.container(key="my_container"):
    for item in st.session_state.my_container["list"]:
        with st.container(key=f"item_{item}"):
            st.write(item)

# 3. Capture the drag and drop event (must be called AFTER rendering)
event = dnd("my_container")

# 4. Apply the move to session state
if event:
    apply_move(event, st.session_state.my_container)
    st.rerun()

```

This ensures the container key and the dictionary key match, resolving the KeyError. According to the [streamlit-dnd documentation](https://discuss.streamlit.io/t/streamlit-dnd-add-drag-and-drop-behavior-to-streamlit-containers/121727), the keys passed to `dnd()` and used in your items dictionary must be the same.

Sources:

- [streamlit-dnd: Add drag-and-drop behavior to Streamlit containers](https://discuss.streamlit.io/t/streamlit-dnd-add-drag-and-drop-behavior-to-streamlit-containers/121727)
