finding string in a list and return it index or -1
<p>Defining a procedure which return an index of item or -1 if the item not in list</p>

<pre><code>def ser(a,b):
    for j in a:
        if j == b:
            return (a.index(b))
        else:
            return -1

print (ser([1,2,3],3))
</code></pre>

<p>It's always return me -1. If i cut the 'else' part, it works. So why ?</p>

<p>In the first iteration of the for loop, it will test if the first element in the array is equal to b. It is not, so the code returns -1 immediately, without testing the other elements of the array.</p>

<p>For your case, the correct code is:</p>

<pre><code>def ser(a,b):
    for j in a:
        if j == b:
            return (a.index(b))
    return -1
</code></pre>

<p>In this way, the code will try all elements in the array, and will return -1 if none of them is equal to b.</p>

python list indexing