Sorting a list of tensors by their length in Pytorch
I have a list of tensors in the form list = [tensor([1,2]), tensor([3, 4, 5])] and would like to order it in descending order based on the length of the tensors. This means the sorted list should look like list = [tensor([3, 4, 5]), tensor([1, 2])].
Using .sort(key=length) does not work, and have also tried using .sort(key= lambda x: len(x)) unsuccessfully.

You should avoid using python built-ins (list) for your variable names.
you can sort like following:
list_tensors = [torch.tensor([1,2]), torch.tensor([3, 4, 5])]

print(sorted(list_tensors, key=lambda x: x.size()[0]))

which will output :
[tensor([1, 2]), tensor([3, 4, 5])]
Or in descending order :
list_tensors = [torch.tensor([1,2]), torch.tensor([3, 4, 5])]

print(sorted(list_tensors, key=lambda x: x.size()[0], reverse=True))

output :
[tensor([3, 4, 5]), tensor([1, 2])]
