Bird
Raised Fist0
NumPydata~10 mins

Slicing rows and columns in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Slicing rows and columns
Start with 2D array
↓
Choose row slice: start:end
↓
Choose column slice: start:end
↓
Extract sub-array
↓
Use or display sliced data
We start with a 2D array, select rows by slicing, then select columns by slicing, and get a smaller sub-array.
Execution Sample
NumPy
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
slice_rows = arr[0:2, :]
slice_cols = arr[:, 1:3]
slice_rows_cols = arr[0:2, 1:3]
This code slices the first two rows and all columns, then slices all rows and columns 1 to 3, then slices rows 0:2 and columns 1:3 combined.
Execution Table
StepOperationRow sliceColumn sliceResulting array
1Original arrayallall[[1 2 3] [4 5 6] [7 8 9]]
2Slice rows 0 to 20:2all[[1 2 3] [4 5 6]]
3Slice columns 1 to 3all1:3[[2 3] [5 6] [8 9]]
4Slice rows 0 to 2 and columns 1 to 30:21:3[[2 3] [5 6]]
5Exit--Slicing complete
💡 Slicing stops after extracting desired rows and columns.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
arr[[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]][[1 2 3] [4 5 6] [7 8 9]]
slice_rowsN/A[[1 2 3] [4 5 6]][[1 2 3] [4 5 6]][[1 2 3] [4 5 6]][[1 2 3] [4 5 6]]
slice_colsN/AN/A[[2 3] [5 6] [8 9]][[2 3] [5 6] [8 9]][[2 3] [5 6] [8 9]]
slice_rows_colsN/AN/AN/A[[2 3] [5 6]][[2 3] [5 6]]
Key Moments - 3 Insights
Why does arr[0:2, :] select the first two rows and all columns?
Because 0:2 means rows starting at index 0 up to but not including 2, and ':' means all columns. See execution_table row 2.
What does arr[:, 1:3] do to the array?
It selects all rows (':') and columns from index 1 up to but not including 3. See execution_table row 3.
How do you slice both rows and columns at the same time?
Use arr[row_slice, column_slice], for example arr[0:2, 1:3] slices rows 0 and 1 and columns 1 and 2. See execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of slicing rows 0 to 2?
A[[1 2 3] [4 5 6]]
B[[2 3] [5 6]]
C[[7 8 9]]
D[[1 2 3] [4 5 6] [7 8 9]]
💡 Hint
Check execution_table row 2 under 'Resulting array'
At which step does the slicing select columns 1 to 3 for all rows?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at execution_table row 3 for column slice 1:3
If you want to slice only the last column for all rows, how would the slicing look?
Aarr[0:2, 0:1]
Barr[2:3, :]
Carr[:, 2:3]
Darr[1:3, 1:3]
💡 Hint
Column slice 2:3 means column index 2 only, see how column slices work in execution_table rows 3 and 4
Concept Snapshot
Slicing rows and columns in numpy:
Use arr[row_start:row_end, col_start:col_end]
Row slice selects rows from start up to but not including end
Column slice selects columns similarly
':' means select all rows or columns
Result is a smaller sub-array
Full Transcript
We start with a 2D numpy array. To slice rows, we use arr[start:end, :], where start and end define the row range. To slice columns, we use arr[:, start:end]. Combining both, arr[row_start:row_end, col_start:col_end] extracts a sub-array. The slices include the start index but exclude the end index. This lets us pick parts of the array easily.

Practice

(1/5)
1. What does the slicing notation array[1:3, 0:2] do on a 2D NumPy array?
easy
A. Selects rows 1 and 2, and columns 0 and 1
B. Selects rows 0 to 3 and columns 0 to 2
C. Selects rows 1 to 3 and columns 0 to 2
D. Selects rows 1 and 3, and columns 0 and 2

Solution

  1. Step 1: Understand row slicing 1:3

    This means select rows starting at index 1 up to but not including index 3, so rows 1 and 2.
  2. Step 2: Understand column slicing 0:2

    This means select columns starting at index 0 up to but not including index 2, so columns 0 and 1.
  3. Final Answer:

    Selects rows 1 and 2, and columns 0 and 1 -> Option A
  4. Quick Check:

    Slice end is excluded = A [OK]
Hint: Remember end index is excluded in slicing [OK]
Common Mistakes:
  • Including the end index in the slice
  • Confusing rows and columns order
  • Using commas incorrectly in slicing
2. Which of the following is the correct syntax to select all rows and columns 2 to 4 (inclusive start, exclusive end) from a NumPy array named arr?
easy
A. arr[2:4, :]
B. arr[:, 2:5]
C. arr[2:5, :]
D. arr[:, 2:4]

Solution

  1. Step 1: Identify rows and columns in slicing

    The first index before comma is rows, second is columns.
  2. Step 2: Select all rows and columns 2 to 4

    All rows means :, columns 2 to 4 means 2:5 because end is excluded.
  3. Final Answer:

    arr[:, 2:5] -> Option B
  4. Quick Check:

    All rows = :, columns 2 to 4 = 2:5 [OK]
Hint: Use ':' for all rows or columns [OK]
Common Mistakes:
  • Swapping rows and columns in slicing
  • Using wrong end index in slice
  • Missing colon for all rows or columns
3. Given the NumPy array:
import numpy as np
arr = np.array([[10, 20, 30, 40],
                [50, 60, 70, 80],
                [90, 100, 110, 120]])
print(arr[0:2, 1:3])

What is the output?
medium
A. [[50 60] [90 100]]
B. [[10 20 30] [50 60 70]]
C. [[20 30 40] [60 70 80]]
D. [[20 30] [60 70]]

Solution

  1. Step 1: Slice rows 0 to 1

    Rows 0 and 1 are selected (indices 0 and 1), so first two rows.
  2. Step 2: Slice columns 1 to 2

    Columns 1 and 2 are selected (indices 1 and 2), so second and third columns.
  3. Final Answer:

    [[20 30] [60 70]] -> Option D
  4. Quick Check:

    Rows 0-1 and columns 1-2 slice = A [OK]
Hint: Remember slicing excludes the end index [OK]
Common Mistakes:
  • Including the end index in slice
  • Mixing up rows and columns
  • Printing the whole array instead of slice
4. What is wrong with this code snippet to select rows 1 to 3 and columns 0 to 2 from a NumPy array data?
slice = data[1:3, 0:2]
medium
A. It selects rows 1 and 2 only, not row 3
B. It causes a syntax error due to variable name 'slice'
C. It selects columns 0, 1, and 2 instead of 0 and 1
D. It selects rows 0 to 3 instead of 1 to 3

Solution

  1. Step 1: Examine row slicing 1:3

    In Python/NumPy slicing, the end index is exclusive, so 1:3 selects rows 1 and 2 only, not row 3.
  2. Step 2: Column slicing 0:2

    Selects columns 0 and 1 (excludes 2).
  3. Final Answer:

    It selects rows 1 and 2 only, not row 3 -> Option A
  4. Quick Check:

    End index excluded in slicing = A [OK]
Hint: Slicing excludes the end index [OK]
Common Mistakes:
  • Assuming slice includes end index
  • Ignoring variable naming conflicts
  • Confusing rows and columns in slicing
5. You have a 5x5 NumPy array arr. How do you extract the middle 3 rows and middle 3 columns as a new array?
hard
A. arr[0:3, 0:3]
B. arr[2:5, 2:5]
C. arr[1:4, 1:4]
D. arr[1:3, 1:3]

Solution

  1. Step 1: Identify middle rows in 5x5 array

    Rows are indexed 0 to 4. Middle 3 rows are indices 1, 2, 3, so slice 1:4.
  2. Step 2: Identify middle columns similarly

    Columns 1, 2, 3 are middle 3 columns, so slice 1:4.
  3. Final Answer:

    arr[1:4, 1:4] -> Option C
  4. Quick Check:

    Middle 3 rows and columns = 1:4 slice [OK]
Hint: Count indices carefully; middle 3 means 1 to 3 inclusive [OK]
Common Mistakes:
  • Using wrong slice end index
  • Selecting too few or too many rows/columns
  • Confusing zero-based indexing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard