Add shifted integers to list
<p>I have a list of integers, which I would like to inflate to include both the current integers, and their values shifted by +1 and -1.</p>

<p>For example, if I have :</p>

<pre><code>l1 = [3, 9, 15]
</code></pre>

<p>I would like the result to be (the order doesn't matter) :</p>

<pre><code>l2 = [2, 3, 4, 8, 9, 10, 14, 15, 16]
</code></pre>

<p>I can achieve it by doing this :</p>

<pre><code>l1 = [3, 9, 15]
l2 = l1[:]
l2.extend([i-1 for i in l1])
l2.extend([i+1 for i in l1])
</code></pre>

<p>However, is there a <strong>shorter / more efficient way</strong> to do this (using <code>numpy</code> if appropriate) ?</p>

<p><em>Please note that I would also like the "inflation" to work asymmetrically and with more than 2 added values, for example with -3, -2 and -1.</em></p>

<p>You may use list comprehension with <a href="http://pythoncentral.io/pythons-range-function-explained/" rel="nofollow"><code>range()</code></a> as:</p>

<pre><code>&gt;&gt;&gt; [item + i for item in l1 for i in range(-1, 2)]
[2, 3, 4, 8, 9, 10, 14, 15, 16]
</code></pre>

python list python-3.x numpy