-1

Let's say I have a list

list = [1,2,1,3,4,5]

How do I find the index of both the 1 from the list using .index() function in python(or any other methods)?

Mike Yung
  • 17
  • 5

1 Answers1

1

Just use enumerate() instead:

>>> lst = [1,2,1,3,4,5]
>>> index_one = [i for i, x in enumerate(lst) if x == 1]
>>> index_one
[0, 2]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75