Applying modulo to all elements of a python list and getting some correct some incorrect elements
<p>I am implementing a function that reduces all the elements of a list modulo 3. Here is what I have:</p>

<pre><code>def reduceMod3(l):
    for i in l:
        l[i] = l[i] % 3
    return l
</code></pre>

<p>when I call this function on L = [1,2,3,4,5,6,7,8,9] I get:</p>

<pre><code>L = [1, 2, 0, 4, 2, 6, 1, 8, 0]
</code></pre>

<p>Why is this? I am trying to figure it out but I'm in a rush. Thanks.</p>

<p>You're list assignment is off, <code>l[i]</code> is not saying 'the value that equals <code>i</code>' but 'position <code>i</code> in list <code>l</code>'. You also don't want to modify a list as you iterate over it. </p>

<p>I think this is more what you want. Creates a new list of mod3 items of the incoming list. </p>

<pre><code>def reduceMod3(l):
    return [i % 3 for i in l]
</code></pre>

python list loops