Say I'd like to take the ith column along with every column from the yth column to last column.


import numpy as np
a = np.random.rand(50).reshape(5,10)

Now, say I'd like to take the 2nd column, along with all columns from #7 through the last.

I can take several slices like so:


However, what is the correct way to do the following?:


This approach currently results in a syntax error.

1 Answers

yatu On Best Solutions

You can use np.r_ to concatenate multiple multiple slices that can be then applied along the second axis:

a[:, np.r_[1, 6:a.shape[1]]]

Quick check:

np.allclose(a[:, np.r_[1, 6:a.shape[1]]],  a[:, [1,6,7,8,9]])
# True