How to make an attntion mask

I want to make a mask to use for attntion etc.
I used to mask with a for loop, but that would be too slow, so I’d like to change

[4,7,2,5,9]

The length of each data like

[1,1,1,1,1, 0,0,0,0,0]
[1,1,1,1,1, 1,1,1,0,0]
[1,1,1,0,0, 0,0,0,0,0]
[1,1,1,1,1, 1,0,0,0,0]
[1,1,1,1,1, 1,1,1,1,1]

I want to convert it so that it can be handled like 0 and 1 data=data*mask like
Is there such a function in pytorch? Also, is there such a method other than for?