0
0
NumPydata~10 mins

Indexing with ellipsis in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Indexing with ellipsis
Start with array
Use ellipsis ... in index
Ellipsis fills missing dimensions
Extract sub-array or element
Return result
Ellipsis (...) in indexing fills in missing dimensions to select parts of a multi-dimensional array easily.
Execution Sample
NumPy
import numpy as np
arr = np.arange(24).reshape(2,3,4)
result = arr[1,...,2]
print(result)
Selects elements from the 3D array at first index 1, all in middle dims, and index 2 in last dim.
Execution Table
StepIndex UsedEllipsis ExpansionSelected ElementsResult Shape
1arr[1,...,2]... expands to :arr[1, :, 2](3,) array with elements [14, 18, 22]
2arr[1,:,2]Direct indexingElements at arr[1,0,2], arr[1,1,2], arr[1,2,2][14, 18, 22]
3Print resultOutput array[14 18 22](3,)
💡 Indexing completes after ellipsis expands and elements are selected.
Variable Tracker
VariableStartAfter IndexingFinal
arr3D array shape (2,3,4)UnchangedUnchanged
resultUndefinedArray with elements [14, 18, 22]Array with elements [14, 18, 22]
Key Moments - 2 Insights
Why does the ellipsis (...) expand to : in arr[1,...,2]?
Because arr has 3 dimensions and the index uses 2 explicit indices (1 and 2), the ellipsis fills the missing dimension with ':' to select all elements in that dimension, as shown in execution_table row 1.
What shape does the result have after indexing with ellipsis?
The result shape is (3,) because the ellipsis selects all elements in the middle dimension, resulting in a 1D array, as seen in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the shape of 'result' after indexing?
A(4,)
B(2,3)
C(3,)
D(1,3,4)
💡 Hint
Check the 'Result Shape' column in execution_table row 3.
At which step does the ellipsis expand to fill missing dimensions?
AStep 3
BStep 1
CStep 2
DNo expansion occurs
💡 Hint
See 'Ellipsis Expansion' column in execution_table row 1.
If we change arr[1,...,2] to arr[...,2], what would be the shape of the result?
A(2,3)
B(3,4)
C(2,3,4)
D(3,)
💡 Hint
Ellipsis at start fills all but last dimension; check how many dims remain after indexing.
Concept Snapshot
Indexing with ellipsis (...) lets you skip specifying some dimensions.
Ellipsis fills missing dimensions with ':' to select all elements there.
Useful for multi-dimensional arrays when you want to fix some dims and select all in others.
Example: arr[1,...,2] means arr[1,:,2] if arr has 3 dims.
Result shape depends on how many dims are fixed vs selected.
Full Transcript
This lesson shows how to use ellipsis (...) in numpy array indexing. The ellipsis fills in missing dimensions with ':' to select all elements in those dimensions. For example, in a 3D array arr with shape (2,3,4), indexing arr[1,...,2] means select the element at index 1 in the first dimension, all elements in the middle dimension, and index 2 in the last dimension. This results in a 1D array of shape (3,) containing elements [14, 18, 22]. The ellipsis helps avoid writing all ':' when selecting slices in multi-dimensional arrays. The execution table traces how the ellipsis expands and what elements are selected step-by-step.