Created
August 9, 2020 20:46
-
-
Save raeq/e6c655d38fea71ea4a19da350b7a6c5e to your computer and use it in GitHub Desktop.
Finds the indices of matching elements in a list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typing | |
def get_indices(the_list: list, test_value: object) -> typing.Iterable[int]: | |
""" | |
Returns the indices of matching list items. | |
Uses a generator to create an iterator. | |
Args: | |
the_list: the list containing search elements | |
test_value: what we want to find | |
Returns: the index of matching list items | |
>>> print(list(get_indices(list("The jolly green giant"), "e"))) | |
[2, 12, 13] | |
""" | |
generator = (key for key, val in enumerate(the_list) if test_value == val) | |
for key in generator: | |
yield key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment