Slicing
Slicing allows you to select a range of elements from a NumPy array. Instead of taking one element at a time with indexing, you can take multiple elements at once.
Indexing selects one position. Slicing selects a range.
NumPy slicing uses the pattern start : stop : step. The start position is included, but the stop position is not included.
What Is Slicing?
Suppose we have this array:
import numpy as np numbers = np.array([10, 20, 30, 40, 50])
If we want only 20, 30, and 40, we could access each value individually:
print(numbers[1]) print(numbers[2]) print(numbers[3])
But NumPy gives us a much simpler way:
print(numbers[1:4])
[20 30 40]
This is called slicing.
Slicing Syntax
The basic syntax is:
array[start:stop]
For example:
numbers[1:4]
Read it as:
Start at index 1 Stop before index 4
Remember that index 4 is not included.
Index: 0 1 2 3 4
Value: 10 20 30 40 50
↑ ↑
start stop
numbers[1:4]
→ 20, 30, 40
Start and Stop
Let's look at several examples.
numbers = np.array([10, 20, 30, 40, 50]) print(numbers[0:3]) print(numbers[1:4]) print(numbers[2:5])
[10 20 30] [20 30 40] [30 40 50]
The important rule is:
start → included stop → excluded
For example:
numbers[1:4] includes indexes
1, 2, 3, but not 4.
Omitting the Start
You don't always need to provide the starting index.
numbers = np.array([10, 20, 30, 40, 50]) print(numbers[:3])
[10 20 30]
When the start is omitted, NumPy starts from the beginning.
numbers[:3] means: start from beginning stop before index 3
Omitting the Stop
You can also omit the ending index.
numbers = np.array([10, 20, 30, 40, 50]) print(numbers[2:])
[30 40 50]
This means:
start at index 2 continue until the end
So:
numbers[2:] → 30, 40, 50
Copying the Whole Range
If both start and stop are omitted, the entire range is selected.
print(numbers[:])
[10 20 30 40 50]
This means:
start → beginning stop → end
Using Step
Slicing can also include a third value called step.
array[start:stop:step]
Example:
numbers = np.array([10, 20, 30, 40, 50]) print(numbers[0:5:2])
[10 30 50]
The step is 2, so NumPy takes every second
element.
Index: 0 1 2 3 4
Value: 10 20 30 40 50
↑ ↑ ↑
take every 2nd element
Different Step Values
A step of 1 takes every element.
print(numbers[0:5:1])
[10 20 30 40 50]
A step of 2 takes every second element.
print(numbers[0:5:2])
[10 30 50]
A step of 3 takes every third element.
print(numbers[0:5:3])
[10 40]
Reverse an Array
A negative step allows us to move backwards through an array.
numbers = np.array([10, 20, 30, 40, 50]) print(numbers[::-1])
[50 40 30 20 10]
The -1 step means:
Move backwards one element at a time.
This is a very common NumPy/Python pattern for reversing an array.
Slicing With Negative Indexes
Negative indexes can also be used inside a slice.
numbers = np.array([10, 20, 30, 40, 50]) print(numbers[-3:])
[30 40 50]
-3 means the third element from the end.
Another example:
print(numbers[:-2])
[10 20 30]
This means everything except the last two elements.
Slicing a 2D Array
Slicing becomes especially useful when working with rows and columns.
data = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
To select the first two rows:
print(data[:2])
[[10 20 30] [40 50 60]]
The stop index is 2, so rows
0 and 1 are selected.
Selecting Rows and Columns
For a 2D array, the general pattern is:
array[row_slice, column_slice]
For example:
data = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
print(data[0:2, 1:3])
[[20 30] [50 60]]
Read this as:
0:2 → rows 0 and 1 1:3 → columns 1 and 2
So we select this part:
10 [20 30] 40 [50 60] 70 80 90
Selecting Multiple Columns
We can select columns while keeping all rows.
data = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
print(data[:, 1:3])
[[20 30] [50 60] [80 90]]
The colon means all rows:
: → all rows 1:3 → columns 1 and 2
Slicing an AI Dataset
Imagine a dataset containing information about students:
students = np.array([
[20, 170, 65],
[22, 175, 70],
[25, 180, 80],
[21, 168, 60]
])
Suppose the columns represent:
Column 0 → Age Column 1 → Height Column 2 → Weight
We can select the height and weight columns:
print(students[:, 1:3])
[[170 65] [175 70] [180 80] [168 60]]
We kept every student but selected only the height and weight features.
This is a very practical use of slicing when preparing data for Machine Learning.
Slicing Training Data
Slicing is also useful when splitting data into parts.
data = np.array([
[1, 10],
[2, 20],
[3, 30],
[4, 40],
[5, 50]
])
training_data = data[:4]
print(training_data)
[[ 1 10] [ 2 20] [ 3 30] [ 4 40]]
Here, the first four rows were selected for the training data.
The final row could then be used separately for testing or demonstration purposes.
Indexing vs Slicing
| Operation | Example | What It Does |
|---|---|---|
| Indexing | numbers[2] |
Gets one element |
| Slicing | numbers[1:4] |
Gets a range of elements |
| Column selection | data[:, 1] |
Gets one column |
| Range selection | data[:, 1:3] |
Gets multiple columns |
The distinction is simple: indexing points to a position; slicing selects a range.
Complete Example
Let's combine the most important slicing concepts.
import numpy as np
students = np.array([
[20, 170, 65],
[22, 175, 70],
[25, 180, 80],
[21, 168, 60]
])
# First two students
print(students[:2])
# Height and weight of every student
print(students[:, 1:3])
# Last two students
print(students[-2:])
# Every second student
print(students[::2])
[[ 20 170 65] [ 22 175 70]] [[170 65] [175 70] [180 80] [168 60]] [[ 25 180 80] [ 21 168 60]] [[ 20 170 65] [ 25 180 80]]
Simple Slicing Rules
| Code | Meaning |
|---|---|
array[1:4] |
Indexes 1, 2, 3 |
array[:3] |
Beginning through index 2 |
array[2:] |
Index 2 through the end |
array[:] |
Entire array |
array[::2] |
Every second element |
array[::-1] |
Reverse the array |
array[:, 1:3] |
All rows, columns 1 and 2 |
Slicing lets you select a range of data efficiently.
NumPy slicing follows the pattern
start:stop:step. The start is included,
while the stop is excluded. You can omit the start or
stop, use negative indexes, use a step to skip elements,
and use ::-1 to reverse an array. With 2D
arrays, slicing can select specific rows, columns, or
both. This becomes extremely useful when preparing
datasets for AI and Machine Learning.