Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
communitycreator

How to slice an array in NumPy

Onyejiaku Theophilus Chidalu

Overview

In Python, an array is used to store multiple items of the same type together. All elements of an array must be of the same kind. Arrays are handled by a module named NumPy.

Slicing an array means to print the elements of an array for a specified range using their index positions.

In Python, index 0 means the first character. For example, the first character of the word PYTHON is P, and its index position is 0. Similarly, the last character is N, while its index is 5.

When you want to slice an array, you must specify which index to start from and which index to stop at.

For instance, we write a code like the one below:

print(array1[0:4])

In the code above, we tell Python that it should help us slice and print the elements of an array called array1, starting from index 0 (i.e. the first character) to index 4, not inclusive.

Example

Now, let us write a simple code to illustrate how to slice an array.

import numpy as np

array1 = np.array([1, 2, 3, 4, 5, 6, 7])

print(array1[0:4])

Explanation

We can see that we can slice the array array1, thereby printing elements from index 0 to index 4. Notice that the last element is 4, but that element is at index 3 of the given array. This explains why the end index element is not included in the slicing operation. If the last index in our code, which is index 4, is included, then the last element of the slice would be 5.

When we specify the range of elements to be sliced, the start index is written before the colon :, while the end index is written after the colon.

When you want the slicing to start or end at the first and last indexes, you need to leave the index values blank.

Example

In the example below, we slice from the beginning and last index of a given array.

import numpy as np

array1 = np.array([1, 2, 3, 4, 5, 6, 7])

# to start at the beginnig index and stop at index 4 (not included)
print('beginning of the array to index 4 of the array is', array1[:4])

# to start at index 2 and end at the end of the array
print('Index 2 to the end of the array is ', array1[2:]) 

You can see from the code above that a blank is left before or after the colon when we want to specify the beginning or end of an index.

Other examples

import numpy as np

array1 = np.array([1, 2, 3, 4, 5, 6, 7])

# to slice from the index 2  from the end to index 1 from the end
print('>>[-3:-1] is given as:', array1[-3:-1])

# using a step size of 2 and slicing from 0 index to 6 index
print('>> [0:6:2] is given as: ', array1[0:6:2])

After we specify the start and end index before and after the colon :, we can add another colon, and the value that comes right after this colon is the step size. For example, for [0:6:2], the step is 2.

RELATED TAGS

numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring