How to make code that's functionally similar to enumerate without actually using enumerate?
<p>I suppose to write a code that prints out the value of a number that occurs twice in the list given, but they don't allow us to use a built in function on python. How would I be able to write it without using enumerate? </p>

<pre><code>def find_second_occurrence(xs,v):
    count = 0
    value = None
    for i, x in enumerate(xs):
        if v == x:
            count += 1
            if count == 2:
                return i
    if (count &lt; 2):
        return value
</code></pre>

<p><code>enumerate(sequence)</code> is pretty much similar to a construct of the form:</p>

<pre><code>for i in range(len(sequence)):
    # get sequence[i] and return i and sequence[i] for all i's
</code></pre>

<p>So, in your code, replacing <code>enumerate</code> altogether could be done by:</p>

<pre><code>for i in range(len(xs)):
    x = xs[i]             
    if v == x:
        count += 1
        if count == 2:
            return i
</code></pre>

<p>Or, without assigning to an <code>x</code> name to temporarily hold the sequence item:</p>

<pre><code>for i in range(len(xs)):           
    if v == xs[i]:
        count += 1
        if count == 2:
            return i
</code></pre>

<p>Creating a little <code>my_enumerate</code> function, is also rather simple:</p>

<pre><code>def my_enumerate(sequence, start=0):
    for i in range(len(sequence)):
        yield start+i, sequence[i]
</code></pre>

<p><code>start</code> has also been defined as to match that as used in <code>enumerate</code> and gets a default value of <code>0</code>.</p>

<p>Rather than <code>yield</code>ing values (if this is mystifying to you), you can create a list (generator comprehensions are similar to <code>yield</code>ing) comprehension and return that instead:</p>

<pre><code>def my_enumerate(sequence, start=0):
    return [(start+i, sequence[i]) for i in range(len(sequence))]
</code></pre>

python list function python-3.x enumerate