- Slice 1-D arrays with
[start:stop:step]. - Use negative indexes and negative steps.
- Slice rows, columns and sub-matrices of 2-D arrays.
- Explain why a slice is a view and how to get an independent copy.
Slicing Syntax
arr[start : stop : step]- start – first index included (default 0).
- stop – index where slicing stops; it is not included (default: end of array).
- step – gap between elements (default 1). A negative step goes backwards.
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70]) print(arr[1:5]) # index 1 to 4 print(arr[4:]) # from index 4 to the end print(arr[:3]) # first three print(arr[::2]) # every second element print(arr[1:6:2])
[20 30 40 50] [50 60 70] [10 20 30] [10 30 50 70] [20 40 60]
Negative Slicing and Reversing
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70]) print(arr[-3:]) # last three print(arr[-5:-2]) print(arr[::-1]) # reversed print(arr[5:1:-2]) # backwards from index 5 to 2
[50 60 70] [30 40 50] [70 60 50 40 30 20 10] [60 40]
Slicing 2-D Arrays
Give one slice per axis, separated by a comma: arr[row_slice, column_slice]. A single colon : means “all”.
import numpy as np
m = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
print(m[0:2, 1:3]) # rows 0-1, columns 1-2
print(m[:, 0]) # first column (all rows)
print(m[1, :]) # second row
print(m[:, -1]) # last column
print(m[::2, ::3]) # rows 0 and 2, columns 0 and 3[[2 3] [6 7]] [1 5 9] [5 6 7 8] [ 4 8 12] [[ 1 4] [ 9 12]]
Slices are Views
Unlike list slicing, a NumPy slice does not copy the data. It is a view of the same memory, so changing the slice changes the original array. Call .copy() when you need independent data (see Array Copy vs View).
import numpy as np arr = np.array([1, 2, 3, 4, 5]) part = arr[1:4] part[0] = 100 print(arr) # original changed! safe = arr[1:4].copy() safe[0] = -1 print(arr, safe) arr[:2] = 0 # assign to a whole slice print(arr)
[ 1 100 3 4 5] [ 1 100 3 4 5] [-1 3 4] [0 0 3 4 5]
Key Takeaways
[start:stop:step]– stop is excluded;[::-1]reverses.- For 2-D arrays use
arr[rows, cols];arr[:, 0]is the first column. - Slices are views of the original data; use
.copy()for independence.
Video : Array Slicing
Question / Answer (15 questions)
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- NumPy Developers. (n.d.). Indexing on ndarrays. NumPy User Guide. https://numpy.org/doc/stable/user/basics.indexing.html
- NumPy Developers. (n.d.). Copies and views. NumPy User Guide. https://numpy.org/doc/stable/user/basics.copies.html
- NumPy Developers. (n.d.). NumPy quickstart. NumPy User Guide. https://numpy.org/doc/stable/user/quickstart.html
- VanderPlas, J. (2016). Python Data Science Handbook. O’Reilly Media. (Free online edition.) https://jakevdp.github.io/PythonDataScienceHandbook/
- McKinney, W. (2022). Python for Data Analysis (3rd ed.). O’Reilly Media. (Open-access online edition.) https://wesmckinney.com/book/