Indexing list of tensors
I have two identical lists of tensors (with different sizes) except that for the first one all of the tensors are assigned to the cuda device. For example:
list1=[torch.tensor([0,1,2]).cuda(),torch.tensor([3,4,5,6]).cuda(),torch.tensor([7,8]).cuda()]
&gt;&gt;&gt; list1
[tensor([0, 1, 2], device='cuda:0'), tensor([3, 4, 5, 6], device='cuda:0'), tensor([7, 8], device='cuda:0')]

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

I want to extract some tensors from the lists according to an array of indices such as:
ind=torch.tensor([0,2])
&gt;&gt;&gt; ind
tensor([0, 2])

So my solution was to do something like that:
np.array(list1)[ind]

np.array(list2)[ind]

My question is why it works with the first list with the tensors defined on the cuda device and gives an error with the second list as shown below:
&gt;&gt;&gt; np.array(list1)[ind]
array([tensor([0, 1, 2], device='cuda:0'),
       tensor([7, 8], device='cuda:0')], dtype=object)

&gt;&gt;&gt; np.array(list2)[ind]
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
ValueError: only one element tensors can be converted to Python scalars


EDIT:
Just to clarify, the error isn't raised because the tensors have different shapes. The following examples illustrate this point:
list3=[torch.tensor([1,2,3]).cuda()]
list4=[torch.tensor([1,2,3]).cuda(),torch.tensor([4,5,6]).cuda()]
list5=[torch.tensor([1,2,3])]
list6=[torch.tensor([1,2,3]),torch.tensor([4,5,6])]

And the results are:
&gt;&gt;&gt; np.array(list3)
array([tensor([1, 2, 3], device='cuda:0')], dtype=object)
&gt;&gt;&gt; np.array(list4)
array([tensor([1, 2, 3], device='cuda:0'),
       tensor([4, 5, 6], device='cuda:0')], dtype=object)
&gt;&gt;&gt; np.array(list5)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
ValueError: only one element tensors can be converted to Python scalars
&gt;&gt;&gt; np.array(list6)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
ValueError: only one element tensors can be converted to Python scalars


np.array trys to convert each of the elements of a list into a numpy array. This is only supported for CPU tensors. The short answer is you can explicitly instruct numpy to create an array with dtype=object to make the CPU case works. To understand what exactly is happening lets take a closer look at both cases.
Case 1 (CUDA tensors)
First note that if you attempt to use np.array on a CUDA tensor you get the following error
np.array(torch.zeros(2).cuda())

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

In your example, numpy tries to convert each element of list1 to a numpy array, however an exception is raised so it just settles on creating an array with dtype=object.
You end up with
np.array([torch.tensor([0,1,2]).cuda(), torch.tensor([3,4,5,6]).cuda(), torch.tensor([7,8]).cuda()])

being just a container pointing to different objects
array([tensor([0, 1, 2], device='cuda:0'),
       tensor([3, 4, 5, 6], device='cuda:0'),
       tensor([7, 8], device='cuda:0')], dtype=object)

Case 2 (CPU tensors)
For CPU tensors, PyTorch knows how to convert to numpy arrays. So when you run
np.array(torch.zeros(2))

you get a numpy array with dtype float32
array([0., 0.], dtype=float32)

The problem comes in your code when numpy successfully converts each element in list2 into a numpy array and then tries to stack them into a single multi-dimensional array. Numpy expects that each list entry represents one row of a multi-dimensional array, but in your case it finds that not all rows have the same shape, so doesn't know how to proceed and raises an exception.
One way to get around this is to explicitly specify that dtype should remain object. This basically tells numpy &quot;don't try to convert the entries to numpy arrays first&quot;.
np.array([torch.tensor([0,1,2]), torch.tensor([3,4,5,6]), torch.tensor([7,8])], dtype=object)

which now gives a similar result to case 1
array([tensor([0, 1, 2]),
       tensor([3, 4, 5, 6]),
       tensor([7, 8])], dtype=object)

