Learning Objectives – after this chapter you will be able to:
  • 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])
Output
[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
Output
[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
Output
[[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)
Output
[  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)

Answer : Slicing extracts a portion of an array using the syntax arr[start:stop:step], where start is included, stop is excluded and step is the gap between elements.

Answer : start defaults to 0, stop defaults to the end of the array (for a positive step), and step defaults to 1.

Answer : [20 30 40 50] – the elements at indexes 1, 2, 3 and 4.

Answer : Use a step of 2: arr[::2].

Answer : Use a step of -1: arr[::-1].

Answer : Use a negative start index: arr[-3:].

Answer : The slice starts at index 5 (60) and moves backwards in steps of 2, stopping before index 1: it selects indexes 5 and 3, giving [60 40].

Answer : Provide a slice for each axis separated by a comma: arr[row_slice, column_slice]. For example m[0:2, 1:3] selects rows 0–1 and columns 1–2.

Answer : Use m[:, 0] – the colon selects all rows and 0 selects the first column.

Answer : List slicing returns a new list (a copy). NumPy slicing returns a view that shares memory with the original array, so modifying the slice also modifies the original.

Answer : Call .copy() on the slice: part = arr[1:4].copy(). The copy has its own data.

Answer : It assigns 0 to every element in the slice (indexes 0 and 1), changing the original array. A single value is broadcast to all selected positions.

Answer : Views avoid copying data, which saves memory and time, especially for large arrays. The trade-off is that changes through the view affect the original.

Answer : Every second row (rows 0 and 2) and every third column (columns 0 and 3), producing a 2 × 2 array.

Answer : m[1, :] uses an integer for the row, so the result is 1-D with shape (4,). m[1:2, :] uses a slice, so the row dimension is kept and the shape is (1, 4).
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.

Q1. What is np.array([1, 2, 3, 4, 5])[1:4]?

Show Answer

Correct Answer : C. [2 3 4]
Indexes 1, 2 and 3 are included; 4 is excluded.

Q2. Which slice reverses an array?

Show Answer

Correct Answer : A. arr[::-1]
A step of -1 walks backwards through the whole array.

Q3. For a 2-D array m, which expression gives the LAST column?

Show Answer

Correct Answer : D. m[:, -1]
All rows (:) and the last column (-1).

Q4. b = a[0:2]; b[0] = 50. What happens to a?

Show Answer

Correct Answer : B. a[0] becomes 50
Slices are views sharing memory with the original.

Q5. What is the shape of m[1:2, :] for a 3 × 4 array?

Show Answer

Correct Answer : C. (1, 4)
Slicing keeps the row dimension, giving one row of four columns.

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.

  1. NumPy Developers. (n.d.). Indexing on ndarrays. NumPy User Guide. https://numpy.org/doc/stable/user/basics.indexing.html
  2. NumPy Developers. (n.d.). Copies and views. NumPy User Guide. https://numpy.org/doc/stable/user/basics.copies.html
  3. NumPy Developers. (n.d.). NumPy quickstart. NumPy User Guide. https://numpy.org/doc/stable/user/quickstart.html
  4. VanderPlas, J. (2016). Python Data Science Handbook. O’Reilly Media. (Free online edition.) https://jakevdp.github.io/PythonDataScienceHandbook/
  5. McKinney, W. (2022). Python for Data Analysis (3rd ed.). O’Reilly Media. (Open-access online edition.) https://wesmckinney.com/book/