From 7df78ad5ed0c3cc1dc464e7e2de06940d74347ed Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sun, 11 Oct 2020 13:45:19 +0300 Subject: [PATCH] Add find_index_of_all --- snippets/find_index.md | 2 +- snippets/find_index_of_all.md | 17 +++++++++++++++++ snippets/index_of_all.md | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 snippets/find_index_of_all.md diff --git a/snippets/find_index.md b/snippets/find_index.md index 8849178ad..9075ea125 100644 --- a/snippets/find_index.md +++ b/snippets/find_index.md @@ -1,6 +1,6 @@ --- title: find_index -tags: list,beginner +tags: list,intermediate --- Returns the index of the first element in the provided list that satisfies the provided testing function. diff --git a/snippets/find_index_of_all.md b/snippets/find_index_of_all.md new file mode 100644 index 000000000..1adaae0a9 --- /dev/null +++ b/snippets/find_index_of_all.md @@ -0,0 +1,17 @@ +--- +title: find_index_of_all +tags: list,intermediate +--- + +Returns the indexes of all elements in the provided list that satisfy the provided testing function. + +- Use `enumerate()` and a list comprehension to return the indexes of the all element in `lst` for which `fn` returns `True`. + +```py +def find_index_of_all(lst, fn): + return [i for i, x in enumerate(lst) if fn(x)] +``` + +```py +find_index_of_all([1, 2, 3, 4], lambda n: n % 2 == 1) # [0, 2] +``` diff --git a/snippets/index_of_all.md b/snippets/index_of_all.md index 846dcc684..08ba78fe9 100644 --- a/snippets/index_of_all.md +++ b/snippets/index_of_all.md @@ -3,7 +3,7 @@ title: index_of_all tags: list,intermediate --- -Returns a list of indices of all the occurrences of an element in a list. +Returns a list of indexes of all the occurrences of an element in a list. - Use `enumerate()` and a list comprehension to check each element for equality with `value` and adding `i` to the result.