compact
This commit is contained in:
18
README.md
18
README.md
@ -31,6 +31,22 @@ def chunk(arr, size):
|
|||||||
chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]
|
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
|
### count_occurences
|
||||||
|
|
||||||
Counts the occurrences of a value in an list.
|
Counts the occurrences of a value in an list.
|
||||||
@ -99,6 +115,7 @@ def spread(arg):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@ -242,6 +259,7 @@ Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Y
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
|
|||||||
16
snippets/compact.md
Normal file
16
snippets/compact.md
Normal file
@ -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 ]
|
||||||
|
```
|
||||||
@ -25,6 +25,7 @@ def spread(arg):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|||||||
@ -6,6 +6,7 @@ Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Y
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user