HOWTO · NumPy
How to Slice 2D Array in NumPy
There are several methods that we can utilize if we want to slice a 2-dimensional sub-array from a 2-dimensional array in Python. These methods include array indexing and numpy.ix_() function
On this page
A NumPy array can be sliced with one index expression for each axis. Use ordinary slices for rectangular regions or regularly spaced rows and columns; use integer arrays when you need specific row and column intersections. The examples below use a 4 × 4 array so you can compare the results directly.
Slice 2D Array With Array Indexing in NumPy
This example builds a 4 × 4 array and prints selected values.
import numpy as np
x = range(16)
x = np.reshape(x, (4, 4))
print(x)
y = x[[[0], [2]], [1, 3]]
print(y)
Output:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[ 1 3]
[ 9 11]]
NumPy uses zero-based indices, so rows 0 and 2 are the first and third rows, and columns 1 and 3 are the second and fourth columns. In x[[[0], [2]], [1, 3]], the row index array has shape (2, 1) and the column index array has shape (2,). Broadcasting pairs each requested row with both columns, producing the four intersections shown in y.
For a rectangular region, use a slice on each axis. For example, x[0:2, 0:2] selects the first two rows and first two columns; x[2:, 2:] starts at row 2 and column 2 and continues to the ends. Slice stops are excluded, so 0:2 includes indices 0 and 1. A step selects regularly spaced positions: x[0::2, 1::2] takes rows 0 and 2 and columns 1 and 3. Basic slicing generally returns a view of x, while integer-array indexing returns a copy.
The general slice notation is start:stop:step. You can leave start or stop out to begin at an axis boundary or continue through its end, and the default step is 1. In a two-dimensional array, the expression before the comma addresses rows and the expression after it addresses columns. Keeping one slice per axis makes it easy to adapt a selection to a different array shape; check each axis independently when adjusting its bounds.
Use integer arrays when the desired positions are listed explicitly rather than described by a regular interval. The nested row list in this example gives those indices a column shape, allowing them to broadcast against the column list and form all requested intersections. If both index arrays instead have matching one-dimensional shapes, NumPy pairs their entries element by element. Use numpy.ix_() in the next section when the desired meaning is every row combined with every column.
Slice 2D Array With the numpy.ix_() Function in NumPy
Use numpy.ix_() when you have separate lists of row and column indices and want every combination between them. It reshapes the one-dimensional index sequences into arrays that broadcast as a cross-product during multidimensional indexing.
import numpy as np
x = range(16)
x = np.reshape(x, (4, 4))
print(x)
y = x[np.ix_([0, 2], [1, 3])]
print(y)
Output:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[ 1 3]
[ 9 11]]
Here, numpy.ix_() receives rows [0, 2] and columns [1, 3], so the result has the same four intersections as the paired-array example. This form makes the cross-product intent explicit and is useful when the index lists are prepared separately. Both this selection and the paired integer-array selection use advanced indexing, so they return copies. Assigning to either selected result does not update x; a basic slice such as x[0:2, 0:2] generally shares data with the original array. Choose the form that matches how your row and column indices are represented.