How to remove an element from a 1-d tensor by index?

kembo: <p>So I have a 1-d tensor <em>T</em> and an index <em>i</em> and need to remove i-th element from a tensor T, much like in pure python T.remove(i).</p>
<p>I’ve tried to do this:</p>
<pre><code class="lang-auto">i = 2
T = torch.tensor([1,2,3,4,5])
T = torch.cat([T[0:i], T[i+1:-1]])
</code></pre>
<p>But it fails to bring in the last element (5 in this case).<br>
Any suggestions?</p>

fangyh: <p>Update, this works</p>
<pre><code class="lang-auto">i = 2
T = torch.tensor([1,2,3,4,5])
T = torch.cat([T[0:i], T[i+1:]])
</code></pre>

ptab: <p>Your solution should read<br>
<code>T = torch.cat([T[0:i], T[i+1:]])</code><br>
or equivalently<br>
<code>T = torch.cat([T[:i], T[i+1:]])</code><br>
(but there is probably a better way to do this)</p>

kembo: <p>Thank you guys, this solves the problem.<br>
But there must be a way less clunky solution, I believe.</p>

mayank4: <p>how to remove by value??</p>

ptrblck: <p>One possible way would be to index the tensor for all other values:</p>
<pre><code class="lang-python">x = torch.arange(10)
value = 5
x = x[x!=value]
</code></pre>

mayank4: <p>I’m getting this error</p>
<pre><code class="lang-auto">Expected object of scalar type Float but got scalar type Long for argument #2 'other'
</code></pre>

ptrblck: <p>Try to create <code>value</code> as a float type.</p>

SamIIT: <p>This won’t work if <code>i = 0</code>. Any other approach?</p>

GabbyChan: <p>How about removing more than 1 elements by indices? Is there a faster way than looping ?</p>
<p>For example</p>
<pre><code class="lang-auto">i = [0,100,2,5,10, 200,500]
T =torch.randn(1000)

T_new =  T [whose elements not equal to elements in i]
</code></pre>
<p>I want to get the T_new without looping over i…</p>

J_Johnson: <p><a class="mention" href="/u/gabbychan">@GabbyChan</a><br>
<code>T_new = torch.cat([T[:i[0]], T[i[0]+1:i[1]], T[i[1]+1:i[2]],...])</code></p>
<p>Does that help?</p>

GabbyChan: <p>Not fast enough I am afraid…</p>

J_Johnson: <p>If <code>i</code> has a lot of values, a loop is probably your best bet. You could try using multi-processing.</p>

Alexey_Stolpovskiy: <p>Hello, I think, A more elegant way will be to use a function like:</p>
<pre><code class="lang-auto">import torch as th

def th_delete(tensor, indices):
    mask = th.ones(tensor.numel(), dtype=th.bool)
    mask[indices] = False
    return tensor[mask]
</code></pre>
<p>It can be used like this:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; a = th.arange(5)
&gt;&gt;&gt; a
tensor([0, 1, 2, 3, 4])
&gt;&gt;&gt; th_delete(a, [0, 3, 4])
tensor([1, 2])
&gt;&gt;&gt; th_delete(a, [1])
tensor([0, 2, 3, 4])
&gt;&gt;&gt; th_delete(a, th.tensor([1, 4]))
tensor([0, 2, 3])
</code></pre>
<p>And even like this:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; a = th.arange(5)
&gt;&gt;&gt; a
tensor([0, 1, 2, 3, 4])
&gt;&gt;&gt; th_delete(a, 1)
tensor([0, 2, 3, 4])
</code></pre>

xanderladd: <p>This is an elegant solution. Thanks.</p>

Adex: <aside class="quote no-group" data-username="ptrblck" data-post="7" data-topic="23109">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>x = x[x!=value]</code></p>
</blockquote>
</aside>
<p>may you suggest another way to do the same operation if value is not a scaler but another torch 1-d array?</p>

J_Johnson: <p>Try this:</p>
<pre><code class="lang-auto">z=torch.rand(100)
print(z.size())
z=z[z&lt;0.5]
print(z.size())
</code></pre>

Omar_AlSuwaidi: <p>Hey!</p>
<p>What if I wanted to index the tensor for all other values when I want to exclude more than one value?<br>
eg:</p>
<pre><code class="lang-auto">x = torch.arange(10)
value1 = 5
value2 = 3
</code></pre>
<pre><code class="lang-auto">x = x[x!=value and x!=3]
# or:
x = x[x not in [value1, value2]]
</code></pre>
<p>do not work <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>This should work:</p>
<pre><code class="lang-python">x = torch.arange(10)
value1 = 5
value2 = 3

x = x[(x!=value1) &amp; (x!=value2)]
x
# tensor([0, 1, 2, 4, 6, 7, 8, 9])
</code></pre>