How to convert unregular python list to tensor

Hi , when I use torch.tensor() to convert one python list to tensor, some problems happen. It seems that only a regular list can be converted , but one list like [1 , 2 ,[3, 4], 5] can not be converted correctly. The bug is “TypeError: an integer is required”. I don’t know how to fix it since I want to pass it between different processes used by torch.distributed.send() and torch.distributed.recv() function

[1 , 2 ,[3, 4], 5] can not be converted correctly.

Well, it’s not a symmetric shape. it’s impossible to have a matrix that has a different number of elements in each row – it doesn’t exist. You can cast this into a vector though if that’s desired:

your_list = [1 , 2 ,[3, 4], 5] 

torch.tensor(list(
    itertools.chain(*[[i] if isinstance(i, int) else i for i in your_list])))

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

Really thank you for your quick reply !!! I have solved my question inspired by your suggestion.