Indexing and Slicing Arrays
Indexing and slicing are the ways you reach into a NumPy array to read or change specific elements — indexing grabs a single value by its position, while slicing pulls out a whole range with a start:stop:step pattern.
Learn Indexing and Slicing Arrays in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll select single elements, use negative indices, slice 1D and 2D arrays, carve out sub-grids, and assign new values straight into a slice.
A one-dimensional array works just like a Python list: each element has a position called an index , and indices start at 0 . Put the index in square brackets to read one value.
Negative indices count backwards from the end, so arr[-1] is the last element and arr[-2] is the second-to-last — handy when you don't know the length.
A slice selects a range of elements using start:stop:step . The start is included, the stop is excluded, and step controls how many you skip.
Expected output: [1 2 3] , [0 1 2] , [5 6 7] , [0 2 4 6] , and the reversed [7 6 5 4 3 2 1 0] .
For a two-dimensional array you give two indices: arr[row, col] . Use a single index like arr[1] to grab a whole row , and a colon like arr[:, 0] to grab a whole column (the colon means "all rows").
Expected output: 1 , 6 , [4 5 6] , [1 4 7] , and 9 .
Combine slices on both axes to cut out a rectangular sub-grid : arr[0:2, 1:3] takes rows 0–1 and columns 1–2. Because slices are views , you can also assign to them — arr[0:2] = 0 overwrites everything in those rows at once.
Expected output: the sub-grid [[2 3] [6 7]] , then [ 0 0 30 40 50] .
Replace each ___ so the program prints the last element, a middle slice, and the first column.
Expected output: 45 , [15 25 35] , [1 3] . (Answers: -1 , 4 , 0 .)
❌ IndexError: index 5 is out of bounds for axis 0 with size 5
You used an index that's past the end. A size-5 array has valid indices 0 through 4.
✅ Fix: use arr[-1] for the last element, or check arr.shape .
❌ Expected one column but got a single number
Writing arr[0] on a 2D array returns the whole first row , not a column.
✅ Fix: use arr[:, 0] — the colon keeps all rows and picks column 0.
From a 4×4 grid, slice out the inner 2×2 block and then zero out the top row.
Lesson 6 complete — you can navigate any array!
You can now index single elements, use negative indices, slice ranges with steps, reach into rows and columns of 2D arrays, carve out sub-grids, and assign values straight through a slice.
🚀 Up next: Boolean & Fancy Indexing — select elements by condition instead of position.
Practice quiz
What index does the first element of a NumPy array have?
- 0
- 1
- -1
- It varies
Answer: 0. NumPy indices are zero-based, so the first element is at index 0.
What does arr[-1] return?
- The first element
- An error
- The last element
- The middle element
Answer: The last element. Negative indices count from the end, so -1 is always the last element.
For arr = [0,1,2,3,4,5,6,7], what does arr[1:4] return?
Slicing includes start (1) and excludes stop (4): indices 1, 2, 3.
In a slice start:stop:step, which is true about stop?
- It is included
- It must be negative
- It is excluded
- It is the step size
Answer: It is excluded. The stop index is exclusive; the slice never includes the stop position.
What does arr[::2] do?
- Reverses the array
- Returns every second element
- Returns the first two elements
- Doubles each element
Answer: Returns every second element. A step of 2 takes every other element starting from the beginning.
For a 2D grid, what does grid[:, 0] select?
- The first row
- The whole first column
- The element (0,0)
- The last column
Answer: The whole first column. The colon means all rows, and 0 picks column 0: the whole first column.
Does a basic slice return a copy or a view?
- A copy
- A view that shares memory
- A new dtype
- Always an error
Answer: A view that shares memory. Basic slicing returns a view; writing to it changes the original array.
What does arr[::-1] return?
- The array unchanged
- Every second element
- The first half
- The array reversed
Answer: The array reversed. A step of -1 walks the array backwards, reversing it.
For grid = [[1,2,3],[4,5,6],[7,8,9]], what is grid[1, 2]?
- 6
- 2
- 8
- 4
Answer: 6. Row 1, column 2 (both zero-based) is the value 6.
How do you make an independent copy instead of a view?
arr.copy() returns a separate array that does not share memory with the original.