modify list elements on a condition
<p>I am new to Python but have C/C++ background.
I am trying to modify a 2D list (list of lists) elements that satisfy a condition.
All I could come up with is the following solution:</p>

<pre><code>tsdata = [[random.random() for x in range(5)] for y in range(5)]
def check(tsdata):
    for i in range(len(tsdata)):
        for j in range(len(tsdata[i])):
            if (tsdata[i][j] &lt; min_value) or (tsdata[i][j]&gt; max_value):
                tsdata[i][j] = 0
check(tsdata)
</code></pre>

<p>I am sure there is a better solution which is actually suitable for Python but can't think of anything else that actually works. </p>

<p>EDIT: Actually, the list tsdata is a function argument and I am trying to modify it so some of your answers do not work. I have edited the code so this is clear.</p>

<p>If you want to handle big arrays, you may want to use <code>numpy</code>. Besides being much more efficient, I think the code is easier to read:</p>

<pre><code>from numpy import random
tsdata = random.random((5, 5))
tsdata[tsdata &lt; min_value] = 0
tsdata[tsdata &gt; max_value] = 0
</code></pre>

python list