Need help understanding this Python list syntax
I'm having trouble understanding what this syntax means in Python:

out = out[lengths - 1, range(len(lengths))]


Why is there a range inside a list? How does that work? For context, I'm training a machine learning model in PyTorch. lengths is a list of the lengths of the input.

I assume lengths is an array of integers. (probably a Numpy array) 

The first index lengths - 1 will give a list of indices that is subtracted by -1.
The second index range(len(lengths)) will give a list of numbers from 0 to the size of lengths.

I don't know what the specific logic is in your code, but in general, you can give a list of indices to pick the data at specific locations.

out = np.array([[1,2,3],[4,5,6],[7,8,9]])
-&gt; array([[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]])

first_idx = [0, 1, 2]
second_idx = [2, 1, 0]
# (0,2), (1,1), (2,0)

out[first_idx, second_idx]
-&gt; array([3, 5, 7])

