Skip to content
Advertisement

how to check non-empty selection?

I am using plotly-dash dropdown menu with multiple selection enabled. so the dropdown menu returned either an empty list when no selection, or a string ( with one selection) or list of strings ( with multiple selection).

How do I check if returned is not empty?

if I use below, it doesn’t work for empty list

if selection is not None:

Thanks for your help.

Advertisement

Answer

I think this should work:

def check_selection(selection):
    if selection:
        if isinstance(selection, list):
            return "Multiple Selections"
        else:
            return "One Selection"
    else:
        return "No Selection"

Empty sequences (like strings and lists) are considered “falsy” in Python, and evaluate to the boolean False. Please let me know if I interpreted your question correctly, and this answer helps.

Advertisement