You could use some rnn util functions:
x = [torch.tensor([0, 1, 2, 3, 4]), torch.tensor([0, 1, 2])]
x = torch.nn.utils.rnn.pack_sequence(x)
out = torch.nn.utils.rnn.pad_packed_sequence(x, batch_first=True)
print(out)
> (tensor([[0, 1, 2, 3, 4],
        [0, 1, 2, 0, 0]]), tensor([5, 3]))

Where the first return value will be the padded tensor, while the second will give you the lengths before padding.