-3

I have an array of integers:

samples = [123,125,125,120,0,132,133,123,123,...,125,0,133,133,120]

I had seen methods to find an element in the array. But those examples are looking for one element with no duplicate in the array. In my case, I would like to find the index of '0' but in my array, I have two '0's in it.

If I only have one '0' in the array, this method will work:

if sample.count(0)==1:
    b = sample.index(0)

and b would contain the position of '0' in the array. If I try it with the array containing two '0's, this wouldn't work any more because of the duplicate. I only want to find the index of the first '0'. How do I solve this?

wengzhe
  • 1
  • 2
  • `sample.index(0)` will give you the index of the first `0` in `sample`, no matter how many `0`s you have in sample (as long as you have at least one) – inspectorG4dget May 08 '14 at 02:43

1 Answers1

1

From the docs:

list.index(x) Return the index in the list of the first item whose value is x. It is an error if there is no such item.

So index does what you want (it returns the first element that you are looking for).

jh314
  • 27,144
  • 16
  • 62
  • 82