isinstance()
to check if the given value is enumerable.list()
or encapsulated in a list accordingly.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']
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️