Slicing Arrays
Slicing is the process of extracting a subset of an array by specifying a range of indices. NumPy arrays can be sliced using the colon :
operator.
Open the Python Shell
Open the Python shell by typing the following command in the terminal.
python3
Import NumPy
NumPy is already installed, you can import it in your Python code:
import numpy as np
Slice Arrays in One Dimension
## create a 1-dimensional array
a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
## slice the array from index 2 to index 5
print(a[2:5])
Output:
[2 3 4]
Slice Arrays in Multiple Dimensions
You can also slice arrays in multiple dimensions.
## create a 2-dimensional array
b = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
## slice the first two rows and first two columns
print(b[:2, :2])
Output:
[[0 1]
[3 4]]