Python Forum
What a difference print() makes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What a difference print() makes
#3
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"))
Mark17 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
What a difference print() makes - by Mark17 - Oct-19-2023, 06:49 PM
RE: What a difference print() makes - by deanhystad - Oct-19-2023, 07:05 PM
RE: What a difference print() makes - by DeaD_EyE - Oct-20-2023, 10:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter + Multiprocessing startmap makes UI Freeze sunny9495 4 1,469 Nov-13-2022, 12:49 PM
Last Post: sunny9495
  Upgrade makes Code Error kucingkembar 6 3,187 Jul-28-2022, 06:44 PM
Last Post: kucingkembar
  help! this 'for' loop makes me crazy lerner50 5 2,963 Jul-09-2019, 03:39 PM
Last Post: perfringo
  Difference between return and print DragonG 9 6,612 Oct-26-2018, 12:06 AM
Last Post: Skaperen
  The number of object makes code slower? fig0 1 2,529 Jan-25-2018, 11:16 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020