0Pricing
Learn AI with Python · Lesson

Indexing, Slicing, and Fancy Indexing

Basic slicing, boolean masking, fancy indexing, np.where() for conditional selection.

Indexing, Slicing, and Fancy Indexing is a free Learn AI with Python lesson on CoddyKit — lesson 2 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 Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Indexing Basics

NumPy arrays use zero-based indexing like Python lists. Negative indices count from the end.

import numpy as np
a = np.array([10, 20, 30, 40, 50])
print(a[0])    # 10
print(a[-1])   # 50

Slicing 1D

Slices use start:stop:step and return a view (not a copy), so modifying a slice changes the original.

print(a[1:4])    # [20 30 40]
print(a[::2])    # [10 30 50]
view = a[1:3]
view[0] = 99
print(a)         # [10 99 30 40 50]

2D Indexing [row, col]

For 2D arrays, index with a single bracket holding both coordinates: m[row, col]. This is cleaner and faster than m[row][col].

m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(m[1, 2])   # 6
print(m[0, 0])   # 1

Slicing Rows and Columns

Use a colon to take an entire axis. m[:, 1] selects column 1 across all rows.

print(m[0, :])    # first row:    [1 2 3]
print(m[:, 1])    # second col:   [2 5 8]
print(m[1:, :2])  # bottom-left block

Boolean Masks

A comparison produces a boolean array of the same shape. Indexing with that mask keeps only the True positions.

a = np.array([5, 12, 7, 20, 3])
mask = a > 8
print(mask)     # [False  True False  True False]
print(a[mask])  # [12 20]

Combining Conditions

Use element-wise & (and) and | (or) with parentheses. The Python keywords and/or do NOT work on arrays.

print(a[(a > 4) & (a < 15)])   # [ 5 12  7]

Assigning Through a Mask

You can write to the masked positions to clean data in place, for example clamping outliers to a ceiling.

a = np.array([5, 12, 7, 20, 3])
a[a > 10] = 10
print(a)   # [ 5 10  7 10  3]

Fancy Indexing with Arrays

Pass an array (or list) of integer indices to select arbitrary elements in any order, possibly with repeats. This returns a copy, not a view.

a = np.array([10, 20, 30, 40, 50])
idx = [4, 0, 2, 2]
print(a[idx])   # [50 10 30 30]

Fancy Indexing in 2D

Provide two index arrays to pick specific (row, col) pairs. Here we grab the diagonal corners.

m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rows = [0, 2]
cols = [0, 2]
print(m[rows, cols])   # [1 9]

Conditional Selection with np.where

np.where(cond, x, y) returns elements from x where the condition is True and from y otherwise, a vectorized if-else.

a = np.array([-2, 5, -7, 3])
print(np.where(a < 0, 0, a))   # [0 5 0 3]  (clip negatives)

np.where to Find Indices

Called with one argument, np.where(cond) returns the indices where the condition holds.

a = np.array([10, 20, 30, 40])
print(np.where(a > 15))   # (array([1, 2, 3]),)

Quick Check

Test your indexing skills.

Recap

Selection techniques:

  • Basic index and slice (slices are views)
  • 2D access m[row, col], full-axis with :
  • Boolean masks with & and |
  • Fancy indexing with arrays of indices (returns a copy)
  • np.where(cond, x, y) for vectorized if-else

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 Learn AI with Python 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 Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Indexing, Slicing, and Fancy Indexing”?

Basic slicing, boolean masking, fancy indexing, np.where() for conditional selection. You practise Learn AI with Python 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 Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 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 Learn AI with Python lesson?

Yes. Every Learn AI with Python 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

  1. Array Creation and Properties
  2. Indexing, Slicing, and Fancy Indexing
  3. Broadcasting and Vectorized Operations
  4. Linear Algebra with NumPy
← Back to Learn AI with Python