Skip to content

Home

Cast to list

Casts the provided value as a list if it's not one.

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

cast_list('foo') # ['foo']
cast_list([1]) # [1]
cast_list(('foo', 'bar')) # ['foo', 'bar']

More like this

Start typing a keyphrase to see matching snippets.