Add head, tail, initial snippets
This commit is contained in:
17
snippets/head.md
Normal file
17
snippets/head.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
title: head
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Returns the head of a list.
|
||||
|
||||
use `lst[0]` to return the first element of the passed list.
|
||||
|
||||
```py
|
||||
def head(lst):
|
||||
return lst[0]
|
||||
```
|
||||
|
||||
```py
|
||||
head([1, 2, 3]); # 1
|
||||
```
|
||||
17
snippets/initial.md
Normal file
17
snippets/initial.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
title: initial
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Returns all the elements of a list except the last one.
|
||||
|
||||
Use `lst[0:-1]` to return all but the last element of the list.
|
||||
|
||||
```py
|
||||
def initial(lst):
|
||||
return lst[0:-1]
|
||||
```
|
||||
|
||||
```py
|
||||
initial([1, 2, 3]); # [1,2]
|
||||
```
|
||||
18
snippets/tail.md
Normal file
18
snippets/tail.md
Normal file
@ -0,0 +1,18 @@
|
||||
---
|
||||
title: tail
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Returns all elements in a list except for the first one.
|
||||
|
||||
Return `lst[1:]` if the list's length is more than `1`, otherwise, return the whole list.
|
||||
|
||||
```py
|
||||
def tail(lst):
|
||||
return lst[1:] if len(lst) > 1 else lst
|
||||
```
|
||||
|
||||
```py
|
||||
tail([1, 2, 3]); # [2,3]
|
||||
tail([1]); # [1]
|
||||
```
|
||||
Reference in New Issue
Block a user