def _nested_sequence_last(data, valid_length):
assert isinstance(data, list)<EOL>if isinstance(data[<NUM_LIT:0>], (mx.sym.Symbol, mx.nd.NDArray)):<EOL><INDENT>F = mx.sym if isinstance(data[<NUM_LIT:0>], mx.sym.Symbol) else mx.ndarray<EOL>return F.SequenceLast(F.stack(*data, axis=<NUM_LIT:0>),<EOL>sequence_length=valid_length,<EOL>use_sequence_length=True)<EOL><DEDENT>elif isinstance(data[<NUM_LIT:0>], list):<EOL><INDENT>ret = []<EOL>for i in range(len(data[<NUM_LIT:0>])):<EOL><INDENT>ret.append(_nested_sequence_last([ele[i] for ele in data], valid_length))<EOL><DEDENT>return ret<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>
Parameters
----------
data : nested container of NDArrays/Symbols
    The input data. Each element will have shape (batch_size, ...)
valid_length : NDArray or Symbol
    Valid length of the sequences. Shape (batch_size,)
Returns
-------
data_last: nested container of NDArrays/Symbols
    The last valid element in the sequence.