# Check if tensor elements in a value list

**URL:** <https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923>\
**Category:** Uncategorized\
**Created:** [January 28, 2020, 4:30pm UTC](https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923 "2020-01-28T16:30:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oyyqofuow](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/oyyqofuow/32/20026_2.png) [@oyyqofuow](https://discuss.pytorch.org/u/oyyqofuow)\
**Post date:** [January 28, 2020, 4:30pm UTC](https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923/1 "2020-01-28T16:30:13Z")

</div>

Hi, is there any way to see if elements in a tensor are in a value list?  
e.g., a tensor a = ([1, 2, 3, 4, 5]), and I want to check if each element in a is in value list [1, 2, 3]  
here we can see that element 4 and 5 are not in value list [1, 2, 3], and element 1 and 2 and 3 are in that value list.

It is actually can be written as: (a == 1) or (a == 2) or (a == 3), while sometimes I don’t want to iterate values in the value list [1, 2, 3] thus I want to do the task without iterating values in the list [1, 2, 3], any function to do that?

---

<div class="post-metadata">

**Author:** ![Cami\_Williams](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cami_williams/32/20004_2.png) [@Cami\_Williams](https://discuss.pytorch.org/u/Cami_Williams)\
**Post date:** [January 28, 2020, 5:37pm UTC](https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923/2 "2020-01-28T17:37:18Z")

</div>

I found this response from @ptrblck in 2018 that could be a solution: [How to judge a Tensor is in a list?](https://discuss.pytorch.org/t/how-to-judge-a-tensor-is-in-a-list/15998/3)

---

<div class="post-metadata">

**Author:** ![iacob](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/8dc957/32.png) [@iacob](https://discuss.pytorch.org/u/iacob)\
**Post date:** [March 24, 2021, 11:06am UTC](https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923/3 "2021-03-24T11:06:47Z")

</div>

You can achieve this with a [for loop](https://stackoverflow.com/q/66779647/9067615):

```python
>>> sum(a==i for i in vals).bool()
tensor([[True, True, False],
        [True, True, False]])

```

---

<div class="post-metadata">

**Author:** ![Saruar\_Alam](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saruar_alam/32/31081_2.png) [@Saruar\_Alam](https://discuss.pytorch.org/u/Saruar_Alam)\
**Post date:** [June 22, 2021, 11:39pm UTC](https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923/4 "2021-06-22T23:39:04Z")

</div>

You can do achieve this way as well

```auto
setensor = torch.randint(0, 10, size = (5, 20))
selist = [1, 2, 4, 6]
print(setensor)
semasks = torch.any(torch.stack([torch.eq(setensor, aelem).logical_or_(torch.eq(setensor, aelem)) for aelem in selist], dim=0), dim = 0)
print(semasks)

```

---

<div class="post-metadata">

**Author:** ![jiangsijia9495](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jiangsijia9495/32/47768_2.png) [@jiangsijia9495](https://discuss.pytorch.org/u/jiangsijia9495)\
**Post date:** [March 21, 2022, 6:51am UTC](https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923/6 "2022-03-21T06:51:44Z")

</div>

You help me a lot!  
Clever solution!

---

<div class="post-metadata">

**Author:** ![fleance](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fleance/32/55926_2.png) [@fleance](https://discuss.pytorch.org/u/fleance)\
**Post date:** [December 24, 2022, 12:12am UTC](https://discuss.pytorch.org/t/check-if-tensor-elements-in-a-value-list/67923/7 "2022-12-24T00:12:36Z")

</div>

You can also achieve this without an explicit for loop:

```auto
t = torch.randint(0, 10, size=( 10, 10))
print(t)
search = torch.tensor([0, 5, 9])
mask = ((search.view(-1, 1) - t.view(-1)).transpose(-1, -2) == 0).sum(dim=-1).view(t.shape) != 0
print(mask.int()) # 1s and 0s for readability =)
print(t[mask].unique()) # matches the search tensor!

```

We just subtract every value in t by every value in search, if the result is 0 the value is the same!
