ifelse(*args, default=None, check=None)[source]#

For a list of inputs, return the first one that meets the condition

By default, returns the first non-None item, but can also check truth value or an arbitrary function.

  • args (any) – the arguments to check (note: cannot supply a single list, use * to unpack)

  • default (any) – the default value to use if no arguments meet the condition

  • check (func) – must be None (check if arguments are not None), bool (check if arguments evaluate True), or a callable (which returns True/False)

Equivalent to next((arg for arg in args if check(arg)), default)


# 1. Standard usage
a = None
b = 3
out = sc.ifelse(a, b)

## Equivalent to:
out = a if a is not None else b

# 2. Boolean usage
args = ['', False, {}, 'ok']
out = sc.ifelse(*args, check=bool)

## Equivalent to:
out = next((arg for arg in args if arg), None)

# 3. Custom function
args = [1, 3, 5, 7]
out = sc.ifelse(*args, check=lambda x: x>5)

## Equivalent to:
out = None
for arg in args:
  if arg > 5:
    out = val
New in version 3.1.5.