animals = ["cat", "cat", "dog", "rabbit", "dog", "dog", "horse", "dog"] #solution 1 def item_indexes(item, sequence): indexes = [] start = 0 item_count = sequence.count(item) for n in range(item_count): indexes.append(sequence.index(item, start)) start = indexes[n]+1 print(indexes) ##solution 2 def item_indexes(item: str, sequence: list): indexes = [] item_count = sequence.count(item) for n in range(item_count): i = sequence.index(item) indexes.append(i + n) del sequence[i] print(indexes) item_indexes("dog", animals)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter