Problem selecting defaults in a multiselect when using a list of objects

I’m working on a page to edit some data. I want to display a multiselect from a list of objects. For example, a query to a database. When I want to display a specific column, I have no problem because I can use format_func. But I have a problem when I want some items to be previously selected. How do I do this?

The following code may illustrate my problem, if we make the effort to imagine that the dictionary is my object.

clients = [
    {
        'id': 1,
        'name': 'Client 1',
    },
    {
        'id': 2,
        'name': 'Client 2',
    },
    {
        'id': 3,
        'name': 'Client 3',
    },
]

defaults = [2, 3]

st.multiselect(
    'Select client',
    options=clients,
    default=???,  # How to set the default values?
    format_func=lambda client: client['name'],
)

The most obvious answer would be to use lists with the name strings. But I need to get selected objects, not a string value.

Thanks in advance!

You use the key argument and session_state.

clients = [
    {
        'id': 1,
        'name': 'Client 1',
    },
    {
        'id': 2,
        'name': 'Client 2',
    },
    {
        'id': 3,
        'name': 'Client 3',
    },
]

if "selected_clients" not in st.session_state:
    st.session_state.selected_clients = [clients[1], clients[2]]

st.multiselect(
    'Select client',
    options=clients,
    format_func=lambda client: client['name'],
    key="selected_clients"
)