From fe368c3673d56dafeceb40bfa2e918f4cb0211c4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 9 Oct 2019 13:14:32 +0300 Subject: [PATCH] Update cast_list.md --- snippets/cast_list.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/snippets/cast_list.md b/snippets/cast_list.md index ae897e5a9..103e0f304 100644 --- a/snippets/cast_list.md +++ b/snippets/cast_list.md @@ -5,17 +5,15 @@ 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. +Use `isinstance()` to check if the given value is enumerable and return it by using `list()` or encapsulated in a list accordingly. ```py def cast_list(val): - if isinstance(val, (tuple, list, set, dict)): return list(val) - elif val: return [val] - else: return [] + 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'] +cast_list('foo') # ['foo'] +cast_list([1]) # [1] +cast_list(('foo', 'bar')) # ['foo', 'bar'] ```