diff --git a/README.md b/README.md index 92c9ab1a9..76bf7fc8d 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,22 @@ def chunk(arr, size): chunk([1,2,3,4,5],2) # [[1,2],[3,4],5] ``` +### compact + +Removes falsey values from a list. + +Use `filter()` to filter out falsey values (False, None, 0, and ""). + +```python + +def compact(arr): + return list(filter(lambda x: bool(x), arr)) + +``` + +``` python +compact([0, 1, False, 2, '', 3, 'a', 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ] +``` ### count_occurences Counts the occurrences of a value in an list. @@ -99,6 +115,7 @@ def spread(arg): + ``` ```python @@ -242,6 +259,7 @@ Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Y ```python + from copy import deepcopy from random import randint diff --git a/snippets/compact.md b/snippets/compact.md new file mode 100644 index 000000000..477001202 --- /dev/null +++ b/snippets/compact.md @@ -0,0 +1,16 @@ +### compact + +Removes falsey values from a list. + +Use `filter()` to filter out falsey values (False, None, 0, and ""). + +```python + +def compact(arr): + return list(filter(lambda x: bool(x), arr)) + +``` + +``` python +compact([0, 1, False, 2, '', 3, 'a', 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ] +``` \ No newline at end of file diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index 924e65a5a..4f3d76b7b 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -25,6 +25,7 @@ def spread(arg): + ``` ```python diff --git a/snippets/shuffle.md b/snippets/shuffle.md index efb12c427..d37e74581 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -6,6 +6,7 @@ Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Y ```python + from copy import deepcopy from random import randint