Add drop and drop_right snippets
This commit is contained in:
19
snippets/drop.md
Normal file
19
snippets/drop.md
Normal 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
19
snippets/drop_right.md
Normal 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) # []
|
||||
```
|
||||
Reference in New Issue
Block a user