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>With numpy is something like this</p>

<pre><code>value=1
p = np.array([3, 9, 15])
p = np.append(p,[[[i+value,i-value] for i in p]])
</code></pre>

<p>with output sorted:</p>

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

<p>but you want with value 2 show for example in <strong>3</strong> [1,3,5] or [1,2,3,4,5] ?</p>

<p>if you want the second options is something like this</p>

<pre><code>value=2
p = np.array([3, 9, 15])
p = np.append(p,[[[i+range(1,value+1),i-range(1,value+1)] for i in p]])
</code></pre>

<p>output sorted:</p>

<pre><code>[1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17]
</code></pre>

python list python-3.x numpy