How to use list as index with pytorch
For example, a 2d tensor:

&gt;&gt;&gt; t = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])


And a list l = [0, 1]

If I execute t[l], then it ends up printing the 0th and first line of t.

But what if I want to use l as an index? I expect to use l to find the element at 0th row and 1th column. In other words, I expect the same result as t[0, 1] or t[0][1].

And I want to use it in more than 2d dimensions as well. Using l with length n as an index to track elements in n dimensions tensor.

IIUC You can do this for given scenario - t[tuple(l)]

t
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
l
[0, 1]

t[tuple(l)]        # equivalent to t[(0,1)] or t[0,1]
tensor(2)

