Replacing Elements in 3 dimensional Python List
<p>I've a Python list if converted to NumPy array would have the following dimensions: (5, 47151, 10)</p>

<pre><code>np.array(y_pred_list).shape 
# returns (5, 47151, 10)

len(y_pred_list)
# returns 5
</code></pre>

<p>I would like to go through every element and replace the element where:</p>

<ul>
<li>If the element >= 0.5 then 1.</li>
<li>If the element &lt; 0.5 then 0.</li>
</ul>

<p>Any idea? </p>

<p>ibredeson's answer is the way to go in your specific case. When you have an array a and want to construct an array b of the same shape which takes only two values, depending on a condition on a, consider using <code>np.where</code> (see <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html" rel="nofollow">the doc here</a>):</p>

<pre><code>import numpy as np
a = np.array([0, 1, 0.3, 0.5])
b = np.where(a &gt; 0.5, 2, 7) # 2 and 7 are the values you want to see in b, whether the 
# corresponding element in a is lesser than 0.5 or not.
&gt;&gt;&gt; b
array([7, 2, 7, 7])
</code></pre>

python arrays list numpy