Skip to Content
ModulesNumpy TutorialIndexing and Slicing in Arrays

Slicing and Indexing Arrays in NumPy

Slicing and indexing are powerful tools in NumPy for accessing and manipulating array elements. With these techniques, you can extract subsets of data, reverse arrays, or select elements based on specific criteria.


Indexing in NumPy

Indexing in NumPy allows you to access individual elements of an array. Indexing starts at 0 for the first element and supports both positive and negative indices.

Examples

import numpy as np # Create a 1D array array = np.array([10, 20, 30, 40, 50]) # Accessing elements using positive indexing print(array[0]) # Output: 10 print(array[3]) # Output: 40 # Accessing elements using negative indexing print(array[-1]) # Output: 50 print(array[-3]) # Output: 30

Indexing in 2D Arrays

In 2D arrays, elements are accessed using [row, column] indexing.

# Create a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Access element at first row, second column print(array_2d[0, 1]) # Output: 2 # Access entire row print(array_2d[1, :]) # Output: [4 5 6] # Access entire column print(array_2d[:, 2]) # Output: [3 6 9]

Slicing in NumPy

Slicing is used to extract subsets of data from arrays. It follows the syntax:

start:stop:step

Slicing in 1D Arrays

# Create a 1D array array = np.array([10, 20, 30, 40, 50]) # Slice elements from index 1 to 3 print(array[1:4]) # Output: [20 30 40] # Slice with a step value print(array[::2]) # Output: [10 30 50] # Reverse the array print(array[::-1]) # Output: [50 40 30 20 10]

Slicing in 2D Arrays

# Create a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Slice rows 0 and 1, and columns 1 and 2 print(array_2d[:2, 1:3]) # Output: # [[2 3] # [5 6]] # Reverse rows print(array_2d[::-1, :]) # Output: # [[7 8 9] # [4 5 6] # [1 2 3]] # Reverse columns print(array_2d[:, ::-1]) # Output: # [[3 2 1] # [6 5 4] # [9 8 7]]

Advanced Slicing Techniques

Boolean Indexing

Boolean indexing allows you to filter elements based on a condition.

# Create an array array = np.array([10, 15, 20, 25, 30]) # Select elements greater than 20 print(array[array > 20]) # Output: [25 30]

Fancy Indexing

Fancy indexing uses arrays of indices to access multiple elements.

# Create an array array = np.array([10, 20, 30, 40, 50]) # Access elements at indices 0, 2, and 4 print(array[[0, 2, 4]]) # Output: [10 30 50]

Try It Yourself

Problem 1: Extract Subsets

Given a 2D array, extract the second row and reverse its elements.

Show Code

import numpy as np # Create a 2D array array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Extract and reverse the second row result = array[1, ::-1] print(result) # Output: [6 5 4]

Problem 2: Filter Elements

Given a 1D array, extract all elements that are even.

Show Code

import numpy as np # Create a 1D array array = np.array([11, 12, 13, 14, 15, 16]) # Extract even elements result = array[array % 2 == 0] print(result) # Output: [12 14 16]

Pyground

Play with Python!

Output:

Last updated on