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>When you write <code>for i in l</code>, you are accessing each element of the list, not the index. Instead, you should write </p>

<pre><code>for i in range(len(l)):
</code></pre>

<p>You can also solve this with a list comprehension:</p>

<pre><code>return [item % 3 for item in l]
</code></pre>

python list loops