NumPy Array Slicing

Array Slicing is the process of extracting a portion of an array.

With slicing, we can easily access elements in the array. It can be done on one or more dimensions of a NumPy array.


Syntax of NumPy Array Slicing

Here's the syntax of array slicing in NumPy:

array[start:stop:step]

Here,

  • start - index of the first element to be included in the slice
  • stop - index of the last element (exclusive)
  • step - step size between each element in the slice

Note: When we slice arrays, the start index is inclusive but the stop index is exclusive.

  • If we omit start, slicing starts from the first element
  • If we omit stop, slicing continues up to the last element
  • If we omit step, default step size is 1

1D NumPy Array Slicing

In NumPy, it's possible to access the portion of an array using the slicing operator :. For example,

In the above example, we have created the array named array1 with 9 elements.

Then, we used the slicing operator : to slice array elements.

  • array1[2:6] - slices array1 from index 2 to index 6, not including index 6
  • array1[0:8:2] - slices array1 from index 0 to index 8, not including index 8
  • array1[3:] - slices array1 from index 3 up to the last element
  • array1[:] - returns all items from beginning to end

Modify Array Elements Using Slicing

With slicing, we can also modify array elements using:

  • start parameter
  • stop parameter
  • start and stop parameter
  • start, stop, and step parameter

1. Using start Parameter

Here, numbers[3:] = 20 replaces all the elements from index 3 onwards with new value 20.

2. Using stop Parameter

Here, numbers[:3] = 20 replaces the first 3 elements with the new value 40.

3. Using start and stop parameter

Here, numbers[2:5] = 22 selects elements from index 2 to index 4 and replaces them with new value 22.

4. Using start, stop, and step parameter

In the above example,

numbers[1:5:2] = 16

modifies every second element from index 1 to index 5 with a new value 16.


NumPy Array Negative Slicing

We can also use negative indices to perform negative slicing in NumPy arrays. During negative slicing, elements are accessed from the end of the array.

Let's see an example.

Output

Using numbers[-3:]- [ 8 10 12]
Using numbers[-5:-2]- [4 6 8]
Using numbers[-1::-2]- [12  8  4]

Here,

  • numbers[-3:] - slices last 3 elements of numbers
  • numbers[-5:-2] - slices numbers elements from 5th last to 2nd last(excluded)
  • numbers[-1::-2] - slices every other numbers elements from the end with step size 2

Reverse NumPy Array Using Negative Slicing

In NumPy, we can also reverse array elements using the negative slicing. For example,

Here, the slice numbers[::-1] selects all the elements of the array with a step size of -1, which reverses the order of the elements.


2D NumPy Array Slicing

A 2D NumPy array can be thought of as a matrix, where each element has two indices, row index and column index.

To slice a 2D NumPy array, we can use the same syntax as for slicing a 1D NumPy array. The only difference is that we need to specify a slice for each dimension of the array.

Syntax of 2D NumPy Array Slicing

array[row_start:row_stop:row_step, col_start:col_stop:col_step]

Here,

  • row_start,row_stop,row_step - specifies starting index, stopping index, and step size for the rows respectively
  • col_start,col_stop,col_step - specifies starting index, stopping index, and step size for the columns respectively

Let's understand this with an example.

# create a 2D array
array1 = np.array([[1, 3, 5, 7], 
                   [9, 11, 13, 15]])

print(array1[:2, :2])

# Output

[[ 1  3]
 [ 9 11]]

Here, the , in [:2, :2] separates the rows of the array.

The first :2 returns first 2 rows i.e., entire array1. This results in

[1  3]

The second :2 returns first 2 columns from the 2 rows. This results in

[9 11]

Example: 2D NumPy Array Slicing

Output

First Two Rows and Columns: 
[[ 1  3]
 [ 9 11]]
Last two Rows and Columns: 
 [[13 15]
 [ 6  8]]

Here,

  • array1[:2, :2] - slices array1 that starts at the first row and first column (default values), and ends at the second row and second column (exclusive)
  • array1[1:3, 2:4] - slices array1 that starts at the second row and third column (index 1 and 2), and ends at the third row and fourth column (index 2 and 3)