30 seconds of python

  Back to Home

cast_list

utilitylistbeginner

Casts the provided value as an array if it's not one.

Use isinstance() to check if the given value is enumerable and return it by using list() or encapsulated in a list accordingly.

def cast_list(val):
  return list(val) if isinstance(val, (tuple, list, set, dict)) else [val]