NumPy Tutorial NumPy Statistics NumPy References

NumPy - Basic Slicing and Indexing



NumPy ndarray objects can be indexed using Python x[obj] syntax, where x is the array. There are three kinds of indexing available in numpy: field access, basic slicing, advanced indexing.

The Basic slicing in numpy extends Python's basic concept of slicing to N dimensions. It occurs when obj is a slice object (constructed by start:stop:step notation), an integer, or a tuple of slice objects and integers. Ellipsis and newaxis objects can also be used.

Example:using slice object

In the example below, basic slice syntax start:stop:step is used to slice the given array Arr. Please note that in Python, all indices are zero-based.

import numpy as np

Arr = np.arange(10)
print("Arr contains:", Arr)

#slicing Arr starting from index=1,
#stopping at index=7 with step=2
SArr = Arr[1:7:2]
print("SArr contains:", SArr)

The output of the above code will be:

Arr contains: [0 1 2 3 4 5 6 7 8 9]
SArr contains: [1 3 5]

Example: slicing object without start, stop and step

The slice object can be constructed without specifying start, end or step. In such cases starting index of the array is considered as start, last index is considered as stop, and 1 is considered as step. Consider the example below.

import numpy as np

Arr = np.arange(10)
print("Arr contains:", Arr)

#slicing Arr starting from start to index=7
SArr1 = Arr[:7]
print("SArr1 contains:", SArr1)

#slicing Arr starting from index=2 to last
SArr2 = Arr[2:]
print("SArr2 contains:", SArr2)

#slicing Arr starting from index=2 to last
#with step=2
SArr3 = Arr[2::2]
print("SArr3 contains:", SArr3)

The output of the above code will be:

Arr contains: [0 1 2 3 4 5 6 7 8 9]
SArr1 contains: [0 1 2 3 4 5 6]
SArr2 contains: [2 3 4 5 6 7 8 9]
SArr3 contains: [2 4 6 8]

Example: slicing object with negative index

Python supports negative indexing and it starts with -1 in backward direction. Therefore, Negative value of start and stop can be interpreted as n+start and n+stop respectively, where n is the number of elements in the corresponding dimension. Consider the example below.

import numpy as np

Arr = np.arange(10)
print("Arr contains:", Arr)

#slicing Arr starting from index=-5 to index=-1
SArr = Arr[-5:-1]
print("SArr contains:", SArr)

The output of the above code will be:

Arr contains: [0 1 2 3 4 5 6 7 8 9]
SArr contains: [5 6 7 8]

Example: slicing array using slice function

A slice object can also be constructed using Python built-in - slice function. Consider the example below.

import numpy as np

Arr = np.arange(10)
print("Arr contains:", Arr)

#slicing Arr starting from index=1 to 
#index=7 with step=2
s = slice(1,7,2)
SArr = Arr[s]
print("SArr contains:", SArr)

The output of the above code will be:

Arr contains: [0 1 2 3 4 5 6 7 8 9]
SArr contains: [1 3 5]

Example: selecting a single element

A single element can be selected from the array by specifying its index number.

import numpy as np

Arr = np.arange(10)
print("Arr contains:", Arr)

print("Arr[2]:", Arr[2])

The output of the above code will be:

Arr contains: [0 1 2 3 4 5 6 7 8 9]
Arr[2]: 2

Example: slicing a 2-D array

In the example below, a 2-D array is sliced.

import numpy as np

Arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print("Arr contains:\n", Arr)

#slice the array from index Arr[1:]
SArr1 = Arr[1:]
print("\nSArr1 contains:\n", SArr1)

#slice the array from index Arr[1:,1:]
SArr2 = Arr[1:,1:]
print("\nSArr2 contains:\n", SArr2)

The output of the above code will be:

Arr contains:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]

SArr1 contains:
 [[4 5 6]
  [7 8 9]]

SArr2 contains:
 [[5 6]
  [8 9]]

Example: slicing a 2-D array using Ellipsis

Slicing can also be done using ellipsis . Ellipsis expands to the number of : objects needed for the selection tuple to index all dimensions. In most cases, this means that length of the expanded selection tuple is x.ndim. Consider the following example.

import numpy as np

Arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print("Arr contains:\n", Arr)

#slice using :
SArr1 = Arr[:,1]
print("\nSArr1 contains:\n", SArr1)

#slice using ellipsis
SArr2 = Arr[...,1]
print("\nSArr2 contains:\n", SArr2)

The output of the above code will be:

Arr contains:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]

SArr1 contains:
 [2 5 8]

SArr2 contains:
 [2 5 8]

Example: slicing a 2-D array using numpy.newaxis

numpy.newaxis can also be used while slicing an array which expand the dimensions of the resulting selection by one unit-length dimension. numpy.newaxis is convenient alias for None. Consider the following example.

import numpy as np

Arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print("Arr contains:\n", Arr)

#slice using :
SArr1 = Arr[:,1]
print("\nSArr1 contains:\n", SArr1)

#slice using np.newaxis
SArr2 = Arr[np.newaxis,:,1]
print("\nSArr2 contains:\n", SArr2)

#slice using np.newaxis
SArr3 = Arr[:,np.newaxis,1]
print("\nSArr3 contains:\n", SArr3)

#slice using np.newaxis
SArr4 = Arr[:,1,np.newaxis]
print("\nSArr4 contains:\n", SArr4)

The output of the above code will be:

Arr contains:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

SArr1 contains:
 [2 5 8]

SArr2 contains:
 [[2 5 8]]

SArr3 contains:
 [[2]
  [5]
  [8]]

SArr4 contains:
 [[2]
  [5]
  [8]]