List Indexes multiple same items


#1

If you have a list with multiple items that are the same, like here there are three 1s:

new_list = [0, 1, 3, 5, 8, 1, 4, 1]

is there a simple way to return the indexes of all the 1s? new_list.index(1) seems to return only the index of the first 1.

Replace this line with your code.


#2

you could write a simple loop:

new_list = [0, 1, 3, 5, 8, 1, 4, 1]

for x in range(0,len(new_list)):
    if new_list[x] == 1:
        print(x)

by using range() we get the indexes, which makes it super easy to print the indexes


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.