Shape returns a list, you can do that in one call.
I.e.,
ndim = t2.shape
Where ndim[0] = nLast3Dim in your code.
You can use the sort function on the slice as I mentioned for cleaner implementation. If you still need help I’ll do it when I get to a computer