This commit is contained in:
Rohit Tanwar
2018-01-19 15:47:06 +05:30
parent 2b9ef1451d
commit d4cf9cb418
4 changed files with 36 additions and 0 deletions

View File

@ -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

16
snippets/compact.md Normal file
View 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 ]
```

View File

@ -25,6 +25,7 @@ def spread(arg):
```
```python

View File

@ -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