Files
30-seconds-of-code/snippets/cast_list.md
Isabelle Viktoria Maciohsek 448cb3b532 Update snippet titles
2022-02-10 22:09:42 +02:00

23 lines
523 B
Markdown

---
title: Cast to list
tags: list,intermediate
firstSeen: 2019-08-20T12:47:43+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---
Casts the provided value as a list if it's not one.
- Use `isinstance()` to check if the given value is enumerable.
- Return it by using `list()` or encapsulated in a list accordingly.
```py
def cast_list(val):
return list(val) if isinstance(val, (tuple, list, set, dict)) else [val]
```
```py
cast_list('foo') # ['foo']
cast_list([1]) # [1]
cast_list(('foo', 'bar')) # ['foo', 'bar']
```