diff --git a/snippets/head.md b/snippets/head.md new file mode 100644 index 000000000..04afaee2d --- /dev/null +++ b/snippets/head.md @@ -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 +``` diff --git a/snippets/initial.md b/snippets/initial.md new file mode 100644 index 000000000..4bf098b10 --- /dev/null +++ b/snippets/initial.md @@ -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] +``` diff --git a/snippets/tail.md b/snippets/tail.md new file mode 100644 index 000000000..d2dc534e2 --- /dev/null +++ b/snippets/tail.md @@ -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] +```