torch_concepts.data.utils.ensure_list

ensure_list(value: Any) List[source]

Ensure a value is converted to a list. If the value is iterable (but not a string or dict), converts it to a list. Otherwise, wraps it in a list.

Parameters:

value – Any value to convert to list.

Returns:

The value as a list.

Return type:

List

Examples

>>> ensure_list([1, 2, 3])
[1, 2, 3]
>>> ensure_list((1, 2, 3))
[1, 2, 3]
>>> ensure_list(5)
[5]
>>> ensure_list("hello")
['hello']
>>> ensure_list({'a': 1, 'b': 2})  
TypeError: Cannot convert dict to list. Use list(dict.values())
or list(dict.keys()) explicitly.