Add cast_list snippet

This commit is contained in:
Angelos Chalaris
2019-08-20 12:47:43 +03:00
parent e40ad26cc8
commit dfcd48ed17

18
snippets/cast_list.md Normal file
View File

@ -0,0 +1,18 @@
---
title: cast_list
tags: utility,list,beginner
---
Casts the provided value as an array if it's not one.
Use `isinstance()` to check if the given value is a list and return it as-is or encapsulated in a list accordingly.
```py
def cast_list(val):
return val if isinstance(val, list) else [val]
```
```py
cast_list('foo'); # ['foo']
cast_list([1]); # [1]
```