Pythonic way to replace list values with upper and lower bound (clamping, clipping, thresholding)?
<p>I want to replace outliners from a list. Therefore I define a upper and lower bound. Now every value above <code>upper_bound</code> and under <code>lower_bound</code> is replaced with the bound value. My approach was to do this in two steps using a numpy array.</p>

<p>Now I wonder if it's possible to do this in one step, as I guess it could improve performance and readability.</p>

<p><em>Is there a shorter way to do this?</em></p>

<pre class="lang-python prettyprint-override"><code>import numpy as np

lowerBound, upperBound = 3, 7

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

arr[arr &gt; upperBound] = upperBound
arr[arr &lt; lowerBound] = lowerBound

# [3 3 3 3 4 5 6 7 7 7]
print(arr)
</code></pre>