NumPy Array Slicing

Introduction

NumPy array slicing is used to extract a specific portion of an array. It allows us to access multiple elements by specifying a start position, end position and step size.

In this chapter, we will learn how to slice 1-D, 2-D and 3-D NumPy arrays.

What is Array Slicing?

Slicing means selecting a range of elements from an array.

Syntax:

array[start:stop:step]
  • start: Index where slicing begins (included).
  • stop: Index where slicing ends (excluded).
  • step: Number of positions to skip between elements.

Example:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50, 60])

print(numbers[1:4])

Output:

[20 30 40]

Here, slicing starts at index 1 and stops before index 4.

1. Slicing from a Starting Index

You can select elements from a specific index to the end of an array by providing only the starting index.

Example:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[2:])

Output:

[30 40 50]

2. Slicing Up to an Ending Index

You can select elements from the beginning of an array up to a specified index.

Example:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[:3])

Output:

[10 20 30]

The ending index 3 is not included.

3. Using Step in Array Slicing

The step value determines how many positions NumPy moves before selecting the next element.

Example:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50, 60])

print(numbers[::2])

Output:

[10 30 50]

Here, 2 means NumPy selects every second element.

4. Negative Slicing

Negative slicing allows you to select elements using negative index positions.

Example:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50, 60])

print(numbers[-4:-1])

Output:

[30 40 50]

Here, slicing starts from index -4 and stops before index -1.

5. Reversing an Array Using Slicing

You can reverse the order of array elements by using a step value of -1.

Example:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[::-1])

Output:

[50 40 30 20 10]

6. Slicing a 2-D Array

In a 2-D array, slicing allows you to select specific rows and columns.

Syntax:

array[row_start:row_stop, column_start:column_stop]

Example:

import numpy as np

marks = np.array([[75, 80, 85],
                  [90, 95, 88],
                  [65, 70, 78]])

print(marks[0:2, 0:2])

Output:

[[75 80]
 [90 95]]

Here, the slicing selects the first two rows and the first two columns.

7. Selecting an Entire Row or Column

You can use : to select all rows or columns.

Selecting an Entire Row:

import numpy as np

sales = np.array([[1000, 1200, 1500],
                  [2000, 2200, 2500],
                  [3000, 3200, 3500]])

print(sales[1, :])

Output:

[2000 2200 2500]

Selecting an Entire Column:

print(sales[:, 1])

Output:

[1200 2200 3200]

8. Slicing a 3-D Array

In a 3-D array, slicing can select specific groups, rows and columns.

Example:

import numpy as np

data = np.array([
    [[10, 20, 30], [40, 50, 60]],
    [[70, 80, 90], [100, 110, 120]]
])

print(data[0, :, 0:2])

Output:

[[10 20]
 [40 50]]

Here, the slicing selects the first group, all its rows and the first two columns.

Student Task: Practice Array Slicing

Use the following array:

import numpy as np

numbers = np.array([10, 20, 30, 40, 50, 60, 70, 80])

Complete these tasks:

  1. Select elements from index 2 to index 5.
  2. Select the first four elements.
  3. Select all elements starting from index 4.
  4. Display every second element.
  5. Select the last three elements using negative indexing.
  6. Reverse the complete array.

Final Student Challenge: Analyze Monthly Sales

A company stores sales figures for three products across three months.

import numpy as np

sales = np.array([
    [1000, 1200, 1500],
    [2000, 2200, 2500],
    [3000, 3200, 3500]
])

Write a Python program to:

  1. Display sales figures of the first two products.
  2. Select the sales figures of all products for the second month.
  3. Display the first two months of the last two products.
  4. Display every second column.
  5. Reverse the columns of the array.

Expected Output:

First Two Products:
[[1000 1200 1500]
 [2000 2200 2500]]

Second Month Sales:
[1200 2200 3200]

Last Two Products - First Two Months:
[[2000 2200]
 [3000 3200]]

Every Second Column:
[[1000 1500]
 [2000 2500]
 [3000 3500]]

Reversed Columns:
[[1500 1200 1000]
 [2500 2200 2000]
 [3500 3200 3000]]

Key Takeaways

  • Slicing extracts a portion of an array.
  • start:stop:step defines the slicing range.
  • The stop index is excluded.
  • Negative indexes select elements from the end.
  • [::-1] reverses an array.
  • 2-D and 3-D arrays support slicing across multiple dimensions.
  • : selects all elements along a particular dimension.

Frequently Asked Questions (FAQs)

1. What is NumPy Array Slicing in Python?

NumPy Array Slicing is used to extract a specific portion of an array by specifying a start index, end index, and step value.

2. How do you perform slicing in NumPy?

You can perform slicing in NumPy using the following syntax: array[start:end:step]. It returns selected elements from the array.

3. What is the syntax of NumPy slicing in Python?

The basic syntax of NumPy slicing is arr[start:end:step], where start specifies the starting index, end specifies the stopping position (excluded), and step determines the interval between selected elements.

4. How do you slice a 2-D NumPy Array?

You can slice a 2-D NumPy Array by specifying row and column ranges. For example, arr[0:2, 1:3] selects the first two rows and the second and third columns.

5. What is the difference between NumPy Array Indexing and Slicing?

Indexing is used to access a single element from an array, whereas slicing is used to access a range or portion of elements from an array.

6. Can we use negative indexing in NumPy Array Slicing?

Yes, NumPy supports negative indexing in slicing. For example, arr[-3:-1] selects elements starting from the third-last element up to, but not including, the last element.

7. How do you slice a NumPy Array in reverse order?

You can reverse a NumPy Array using arr[::-1]. The negative step value selects elements from the end towards the beginning.

8. What is the difference between slicing a 1-D and a 2-D NumPy Array?

In a 1-D NumPy Array, slicing uses a single range, such as arr[1:4]. In a 2-D NumPy Array, slicing uses separate ranges for rows and columns, such as arr[0:2, 1:3].

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top