Oct-20-2023, 10:24 PM
As a side note, you could iterate over the
enumerate(list1)
and then you get (index, list_element)
.list1 = ["cat", "dog", "cat", "dog"] def get_all_indicies(iterable, item): for index, elemnt in enumerate(iterable): if item == elemnt: yield index all_indicies = list(get_all_indicies(list1, "cat"))Otherwise, you can use
list1.index("item_to_find", START_INDEX)
.list1 = ["cat", "dog", "cat", "dog"] def get_all_indicies(list_like, item): index = 0 while True: try: index = list_like.index(item, index) yield index except ValueError: return index += 1 list(get_all_indicies(list1, "cat"))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!