How to append an int tensor to a list tensor?

In python, I can use:

A = [1,2]
B = 3
A.append(B)

to get [1,2,3]. However, In pytorch,

A = torch.tensor([1,2])
B = torch.tensor(3)

How to get:

tensor([1,2,3])

Thank you very much

Hello. I believe it could be done in many ways. A reasonable choice for me would be .cat method. Just make sure that the tensor B is also 1-dimensional as A.
This should work:

A = torch.tensor([1,2])
B = torch.tensor([3])
torch.cat((A,B))

Your B tensor is zero dimesional, so you can’t use torch.cat() function to concatenate them.

1)Concatenate them as python array and convert them to tensor
2)Add dimension with unsqueeze() method and use torch.cat()

I use 2) and solve my problem. Thanks

1 Like