Replace all negative values in a list
<p>I'm trying to solve this problem on codewars and I'm completely stumped:</p>

<pre><code>y = [-1, -1, 2, 8, -1, 4]
z = [1,3,5]
#to create [1,3,2,8,5,4]
</code></pre>

<p>How would I do this?</p>

<p>I tried to do:</p>

<pre><code>for e in range(len(y)):
    try:
        if y[e] &lt; 0:
            y[e] = z[e]
    except:
        pass
</code></pre>

<p>But this would only work if the negatives correspond to what is in z.</p>

<p>Your code:</p>

<pre><code>for e in range(len(y)):
    if y[e] &lt; 0: y[e] = z[e]
</code></pre>

<p>The problem with your code is let's say the 5th element is negative. You are attempting to set the 5th element of <code>y</code> to the 5th element of <code>z</code>. Instead, you should use a counter variable to track which element to substitute.</p>

<pre><code>element_z_index = 0
for e in range(len(y)):
    if y[e] &lt; 0:
        y[e] = z[element_z_index]
        element_z_index += 1
</code></pre>

python python-3.x