The squeeze was actually in the case where mask could be a 4D array with the last dimension of size 1, when slicing a 4D volume for example. That was an old thing from an homework, so I guess I'll use your version from now on, it's simpler.

Regarding the code you wrote, newvol has the value of vol where mask is 1 and 0 elsewhere. Since it doesn't return anything, I guess vol is implicitly modified by border effect? In that snippet, newvol is never used again, but it should be the masked array.

Will np.where make the dimension match if the mask has less dimension than vol (3D mask for example on 4D volume)? I guess I'll make some test for myself to improve my own version for personnal use.