How do I preform a check only once?
<p>So I have this question that I have to do for homework.</p>

<p>Next, write ind( e, L ). Here is its description:</p>

<p>Write ind(e, L), which takes in a sequence L and an element e. L might be a string or, more generally, a list. Your function ind should return the index at which e is first found in L. Counting begins at 0, as is usual with lists. If e is NOT an element of L, then ind(e, L) should return the integer equal to len(L). Here are a few examples:</p>

<pre><code>ind(42, [ 55, 77, 42, 12, 42, 100 ]) returns 2
ind(42, range(0,100)) returns 42
ind('hi', [ 'hello', 42, True ]) returns 3
ind('hi', [ 'well', 'hi', 'there' ]) returns 1 
ind('i', 'team') returns 4 
ind(' ', 'outer exploration') returns 5
</code></pre>

<p>In this last example, the first input to ind is a string of a single space character, not the empty string.</p>

<p>Hint: Just as you can check whether an element is in a sequence with</p>

<p>if e in L:
you can also check whether an element is not in a sequence with</p>

<p>if e not in L:
This latter syntax is useful for the ind function! As with dot, ind is probably most similar to mylen from the class examples.</p>

<p>And here it once of the many codes I have written for this problem.</p>

<pre><code>def ind( e, L):
  num = 0
  if e not in L and num == 0:
    return len(L)
  else:
    if e == L[0]:
      num += 1 + ind( e, L[1:] )
      return num
    else:
      return ind( e, L[1:] )
</code></pre>

<p>So the problem is that everytime e is no longer in the list.  It takes the length of the remainder of the list and adds that to num.  How do I fix this???</p>

<p>When working with indicies in sequence, Python has great built-in named <a href="https://docs.python.org/2/library/functions.html#enumerate" rel="nofollow"><code>enumerate</code></a> which helps a lot with keeping track of index in consise and simple manner.</p>

<blockquote>
  <p>Return an enumerate object. <code>sequence</code> must be a sequence, an iterator, or some other object which supports iteration. The <code>next()</code> method of the iterator returned by <code>enumerate()</code> returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence</p>
</blockquote>

<p>Sample:</p>

<pre><code>seq = ['hello', 42, True]
for index, obj in enumerate(seq):
    print index, obj
</code></pre>

<p>Output:</p>

<pre><code>0 hello
1 42
2 True
</code></pre>

<p>We can use it to ease up processing significantly. Since we can iterate over <code>L</code> and retrieve both index and value of current element, we have to do do comparison with requested value now. When value is found, we may simply return it and skip searching through rest of sequence</p>

<pre><code>def ind(e, L):
    for index, value in enumerate(L):
        if value == e:
            return index
</code></pre>

<p>What's missing now is case when value is not found, but adding support for it is fairly simple:</p>

<pre><code>def ind(e, L):
    for index, value in enumerate(L):
        if value == e:
            return index
    return len(L)  # this will be executed only if any value wasn't found earlier
</code></pre>

<p>It passes all testcases:</p>

<pre><code>assert ind(42, [ 55, 77, 42, 12, 42, 100 ]) == 2
assert ind(42, range(0,100)) == 42
assert ind('hi', [ 'hello', 42, True ]) == 3
assert ind('hi', [ 'well', 'hi', 'there' ]) == 1 
assert ind('i', 'team') == 4 
assert ind(' ', 'outer exploration') == 5
</code></pre>

<p>And bonus, with using all tricks Python provides:</p>

<pre><code>def ind(e, L):
    return next((idx for idx, obj in enumerate(L) if obj == e), len(L))
</code></pre>

<p>Have fun with figuring out what happens here :) </p>

python python-2.7