Skip to main content
Documents
basicsArray CreationArray OperationsArray Computation & AnalysisLinear AlgebraRandom ProbabilityData Input/Output & Conversion

NumPy Array Slicing

NumPy array slicing is a technique used to extract a portion of an array, enabling efficient manipulation of large datasets. It allows for accessing, modifying, or analyzing specific sections of the array without copying the data, enhancing both performance and memory utilization.

Usage

Array slicing is utilized when you need to work with a subset of an array, whether for computation, modification, or analysis purposes. The slicing syntax follows the format array[start:stop:step], where start is the index to begin the slice, stop is the index to end the slice (exclusive), and step defines the interval between elements.

import numpy as np

# Syntax
array_slice = array[start:stop:step]

This syntax allows you to create a view of the original array with the specified range and step. Note that modifying this slice will affect the original array since it's a view, not a copy.

Examples

1. Basic Slicing

import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
slice_arr = arr[2:5]
# slice_arr contains elements [2, 3, 4]

Here, slice_arr contains elements [2, 3, 4], which are sliced from the original array arr starting at index 2 and ending before index 5. Remember, array indexing starts at 0.

2. Slicing with Step

import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
slice_arr = arr[1:8:2]
# slice_arr will have elements [1, 3, 5, 7]

In this example, slice_arr will have elements [1, 3, 5, 7], extracted from the original array with a step of 2, starting from index 1 up to index 8.

3. Multi-dimensional Array Slicing

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
slice_arr = arr[0:2, 1:3]
# slice_arr yields [[2, 3], [5, 6]]

This example slices a 2D array to yield [[2, 3], [5, 6]], capturing elements from the first two rows and the last two columns.

4. Negative Index Slicing

import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
slice_arr = arr[-3:]
# slice_arr contains elements [7, 8, 9]

Using negative indices allows slicing from the end of the array, offering flexibility in accessing elements.

5. Reversing an Array

import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
slice_arr = arr[::-1]
# slice_arr contains elements [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

A step of -1 can be used to reverse the array.

Tips and Best Practices

  • Use slicing to avoid copying. Slicing provides a view of the original data, which is more efficient than copying data.
  • Omit parameters for defaults. Omitting start, stop, or step will use default values (start=0, stop=end, step=1), simplifying your code. For example, arr[:] will return the entire array.
  • Indexing errors. Ensure your indices are within the bounds of the array to prevent runtime errors.
  • Utilize negative indices. Negative indices can be utilized to slice from the end of the array, offering flexibility in accessing elements.
  • Consider performance benefits. Slicing is efficient for large arrays in terms of both time and memory usage, providing a significant advantage over list slicing in Python.
  • Slicing with Ellipsis. Use ... to slice higher-dimensional arrays conveniently, maintaining dimensions.