Add drop and drop_right snippets

This commit is contained in:
Angelos Chalaris
2020-03-10 21:59:41 +02:00
parent 458f5606cf
commit 87f1101d80
2 changed files with 38 additions and 0 deletions

19
snippets/drop.md Normal file
View File

@ -0,0 +1,19 @@
---
title: drop
tags: list,beginner
---
Returns a list with `n` elements removed from the left.
Use slice notation to remove the specified number of elements from the left.
```py
def drop(a, n = 1):
return a[n:]
```
```py
drop([1, 2, 3]) # [2, 3]
drop([1, 2, 3], 2) # [3]
drop([1, 2, 3], 42) # []
```

19
snippets/drop_right.md Normal file
View File

@ -0,0 +1,19 @@
---
title: drop_right
tags: list,beginner
---
Returns a list with `n` elements removed from the right.
Use slice notation to remove the specified number of elements from the right.
```py
def drop_right(a, n = 1):
return a[:-n]
```
```py
drop_right([1, 2, 3]) # [1, 2]
drop_right([1, 2, 3], 2) # [1]
drop_right([1, 2, 3], 42) # []
```