From 1afbd00f7e615288e72b09e6c28879b14e8780c1 Mon Sep 17 00:00:00 2001 From: lanzhiwang Date: Wed, 9 Oct 2019 17:14:06 +0800 Subject: [PATCH] fix cast_list --- snippets/cast_list.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/snippets/cast_list.md b/snippets/cast_list.md index 522240914..d08d5b58c 100644 --- a/snippets/cast_list.md +++ b/snippets/cast_list.md @@ -9,10 +9,13 @@ Use `isinstance()` to check if the given value is a list and return it as-is or ```py def cast_list(val): - return val if isinstance(val, list) else [val] + if isinstance(data, (tuple, list, set, dict)): return list(data) + elif data: return [data] + else: return [] ``` ```py cast_list('foo'); # ['foo'] cast_list([1]); # [1] +cast_list(('foo', 'bar')); # ['foo', 'bar'] ```