How to check if any element in a Python iterable is True


Python tip:

You can use any() to check if any element in iterable is True:

def allow_access(role):
    allow_access_for_roles = [role == "superuser", role == "owner", role == "supervisor"]

    if any(allow_access_for_roles):
        print("Access allowed")
    else:
        print("Access denied")


allow_access("superuser")
# -> Access allowed

allow_access("member")
# -> Access denied