Indexing, Slicing, and Fancy Indexing
Access and modify array elements with advanced indexing techniques.
Indexing, Slicing, and Fancy Indexing is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Basic Indexing
Access elements with zero-based integer indices. Negative indices count from the end. Multi-dimensional arrays use comma-separated indices.
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr[0, 2]) # 3
print(arr[-1, -1]) # 9
print(arr[1]) # [4 5 6]Slicing
Slicing syntax start:stop:step returns a view. Applies independently on each dimension.
import numpy as np
m = np.arange(16).reshape(4,4)
print(m[1:3, 1:3])
# [[5 6]
# [9 10]]
print(m[::2, ::2]) # every other row and columnBoolean Indexing (Masking)
Pass a boolean array of the same shape to select elements where True.
import numpy as np
arr = np.array([1, -2, 3, -4, 5])
mask = arr > 0
print(mask) # [ True False True False True]
print(arr[mask]) # [1 3 5]
arr[~mask] = 0 # set negatives to 0
print(arr) # [1 0 3 0 5]Fancy Indexing with Integer Arrays
Pass an array of integer indices to select multiple specific elements.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
idx = np.array([0, 2, 4])
print(arr[idx]) # [10 30 50]
# 2-D fancy indexing:
m = np.arange(9).reshape(3,3)
print(m[[0,2], [0,2]]) # [m[0,0], m[2,2]] = [0, 8]np.where for Indexed Selection
np.where(condition) (one argument) returns indices where condition is True.
import numpy as np
arr = np.array([5, 2, 8, 1, 9])
idx = np.where(arr > 4)[0]
print(idx) # [0 2 4]
print(arr[idx]) # [5 8 9]np.take and np.put
np.take(arr, indices) selects elements (supports flat indexing). np.put(arr, indices, values) inserts values.
import numpy as np
arr = np.array([10, 20, 30, 40])
print(np.take(arr, [1, 3])) # [20 40]
np.put(arr, [0, 2], [99, 88])
print(arr) # [99 20 88 40]np.nonzero
np.nonzero(arr) returns a tuple of arrays — one per dimension — of the indices of non-zero elements.
import numpy as np
arr = np.array([[0,1,0],[2,0,3]])
rows, cols = np.nonzero(arr)
print(rows, cols) # [0 1 1] [1 0 2]Ellipsis Indexing
The ... (Ellipsis) expands to as many : as needed to cover remaining dimensions.
import numpy as np
arr = np.ones((2,3,4,5))
print(arr[0, ..., 2].shape) # (3, 4) — first dim=0, last dim=2
print(arr[..., 0].shape) # (2, 3, 4) — last dim=0np.ix_ for Outer Indexing
np.ix_ constructs an open mesh from multiple 1-D sequences for outer-product-style fancy indexing.
import numpy as np
m = np.arange(25).reshape(5,5)
rows = [0, 2, 4]
cols = [1, 3]
print(m[np.ix_(rows, cols)])
# [[ 1 3]
# [11 13]
# [21 23]]Assigning to Fancy Index
You can assign to a fancy-indexed selection in-place.
import numpy as np
arr = np.zeros(5)
arr[[1, 3]] = [10, 20]
print(arr) # [ 0. 10. 0. 20. 0.]
# Broadcast a scalar:
arr[[0, 2, 4]] = 99
print(arr) # [99. 10. 99. 20. 99.]Difference: View vs Copy in Indexing
Basic/slice indexing → view (modifying changes original). Fancy/boolean indexing → copy (modifying does NOT change original).
import numpy as np
arr = np.arange(5)
view = arr[1:4] # view
view[0] = 99
print(arr) # [0 99 2 3 4]
copy = arr[[1,2,3]] # fancy → copy
copy[0] = 0
print(arr) # unchangedQuick Check
Does boolean/fancy indexing return a view or a copy?
Recap
Use [row, col] for single elements, start:stop:step for slices (views), boolean arrays for masking (copies), and integer arrays for fancy indexing (copies). Use np.where to get indices, np.ix_ for outer meshes.
Frequently asked questions
Is the “Indexing, Slicing, and Fancy Indexing” lesson free?
Yes — the full text of “Indexing, Slicing, and Fancy Indexing” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Indexing, Slicing, and Fancy Indexing”?
Access and modify array elements with advanced indexing techniques. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Indexing, Slicing, and Fancy Indexing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- NumPy Arrays and dtypes
- Array Operations and Broadcasting
- Indexing, Slicing, and Fancy Indexing
- Linear Algebra with NumPy