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>If you are sure that number of negative numbers is always equal with <code>z</code> you can convert <code>z</code> to an iterable and use a list comprehension for creating your new list:</p>

<pre><code>In [9]: z = iter(z)

In [10]: [next(z) if i &lt; 0 else i for i in y]
Out[10]: [1, 3, 2, 8, 5, 4]
</code></pre>

<p>Note that if the length of <code>z</code> is shorter than number of negative numbers it will raise an <code>StopIteration</code> error.</p>

python python-3.x