Python, create new list based on condition applied to an existing list of same length
<p>Ok, I'm sure there is a very easy way to do this, but I'm rusty in python and I can't work out the pythonic way to do this.</p>

<p>I have a list, representing the hours of the day:</p>

<pre><code>import numpy as np
hourOfDay = np.mod(range(0, 100), 24)
</code></pre>

<p>Then I want to create a new list which is a larger value <code>0.4</code>, when the hour is between <code>7</code> and <code>22</code>, and <code>0.2</code> otherwise.</p>

<p>There are several related posts <a href="http://stackoverflow.com/questions/15147696/python-how-to-create-a-new-list-based-on-existing-list-without-certain-objects">here</a> and <a href="http://stackoverflow.com/questions/7406448/python-create-a-new-list-from-a-list-when-a-certain-condition-is-met">here</a>, but they're not quite what I want (they end up with a shorter list, I want the same-length list).</p>

<p>Assuming I needed to use list comprehension I tried this:</p>

<pre><code>newList = [0.4 for hour in hourOfDay if hour &lt;= 7 or hour &gt;= 22 else 0.2]
</code></pre>

<p>One of the alternative approach is to use <code>map()</code> as:</p>

<pre><code>map(lambda x: 0.4 if 7 &lt;= x &lt;= 22 else 0.2, hourOfDay)
</code></pre>

python list numpy