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>The <code>else</code> block is executed after the first iteration does not fulfill <code>j == b</code>. </p>

<p>You're better off moving the else block to the <code>for</code> which executes if the item is not found after the <code>for</code> loop is <em>exhausted</em>:</p>

<pre><code>def ser(a,b):
    for j in a:
        if j == b:
            return (a.index(b))
    else: # or put default return on this line
         return -1
</code></pre>

<p>More importantly, You could also check for containment using <code>b in a</code> without needing to iterate through the list.</p>

python list indexing